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
- 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).
- 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).
T **toconst T **, notT *toconst T *.