Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:47:03+00:00 2026-06-17T18:47:03+00:00

I’m very much confused about the const keyword. I have a function accepting an

  • 0

I’m very much confused about the const keyword. I have a function accepting an array of strings as input parameter and a function accepting a variable number of arguments.

void dtree_joinpaths(char* output_buffer, int count, ...);
void dtree_joinpaths_a(char* output_buffer, int count, const char** paths);

dtree_joinpaths internally invokes dtree_joinpaths_a after it has built an array of strings from the argument list.

void dtree_joinpaths(char* output_buffer, int count, ...) {
    int i;
    va_list arg_list;
    va_start(arg_list, count);
    char** paths = malloc(sizeof(char*) * count);
    for (i=0; i < count; i++) {
        paths[i] = va_arg(arg_list, char*);
    }
    va_end(arg_list);
    dtree_joinpaths_a(output_buffer, count, paths);
}

But the gcc compiler gives me the following error message:

src/dtree_path.c: In function 'dtree_joinpaths':
src/dtree_path.c:65: warning: passing argument 3 of 'dtree_joinpaths_a' from incompatible pointer type

When I change char** paths = malloc(count); to const char** paths = malloc(count);, this error is not showing up anymore. What I don’t understand is, that

  1. I thought a pointer to an address can always be casted to a const pointer, but not the other way round (which is what is happening here imo).
  2. This example works: http://codepad.org/mcPCMk3f

What am I doing wrong, or where is my missunderstanding?


Edit

My intent is to make the memory of the input data immutable for the function. (in this case the paths parameter).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:47:04+00:00Added an answer on June 17, 2026 at 6:47 pm

    The reason char ** -> const char** is a “dangerous” conversion is the following code:

    const char immutable[] = "don't modify this";
    
    void get_immutable_str(const char **p) {
        *p = immutable;
        return;
    }
    
    int main() {
        char *ptr;
        get_immutable_str(&ptr); // <--- here is the dangerous conversion
        ptr[0] = 0;
    }
    

    The above code attempts to modify a non-modifiable object (the global array of const char), which is undefined behavior. There is no other candidate in this code for something to define as “bad”, so const-safety dictates that the pointer conversion is bad.

    C does not forbid the conversion, but gcc warns you that it’s bad. FYI, C++ does forbid the conversion, it has stricter const-safety than C.

    I would have used a string literal for the example, except that string literals in C are “dangerous” to begin with — you’re not allowed to modify them but they have type array-of-char rather than array-of-const char. This is for historical reasons.

    I thought a pointer to an address can always be casted to a const pointer

    A pointer-to-non-const-T can be converted to a pointer-to-const-T. char ** -> const char** isn’t an example of that pattern, because if T is char * then const T is char * const, not const char * (at this point it’s probably worthwhile not writing the const on the left any more: write char const * and you won’t expect it to be the same as T const where T is char *).

    You can safely convert char ** to char * const *, and (for reasons that require a little more than just the simple rule) you can safely convert char ** to char const * const *.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am confused How to use looping for Json response Array in another Array.
I have an array which has BIG numbers and small numbers in it. I
I need a function that will clean a strings' special characters. I do NOT
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.