Do excuse me to the basic”ness” of this question. I am at a loss with pointers at times. I have a char * but I need to convert it to a char * const * to be able to correctly use it in the fts() function. How do i do that?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
You are not supposed to do that kind of conversion, because the types are not compatible.
About pointers and pointers to pointers
char *is a pointer to a string of characters, whereaschar **is an pointer to a pointer to a string of characters. (the const is a bonus). This probably means that instead of supplying a string of characters, you should provide an array of string of characters.Those two things are clearly incompatible. Don’t mix them with a cast.
About the fts_* API
To find the solution to your problem, we need to read the fts_* function API (e.g. at http://linux.die.net/man/3/fts), I see that:
with your
char * const *parameterpath_argv, the description explains:which confirms that the
fts_openfunction is really expected a collection of paths, not one only path.So I guess you need to pass to it something like the following:
About the
constTypes in C and C++ are read from right to left. So if you have:
char *: pointer to charchar const *: pointer to const char (i.e. you can’t modify the pointed string, but you can modify the pointer)const char *: the same aschar const *char * const: const pointer to char (i.e. you can modify the pointed string, but you can’t modify the pointer)char **: pointer to pointer to charchar * const *: pointer to const pointer to char (i.e. you can modify the pointer, and you can modify the strings of char, but you can’t modify the intermediary pointerIt can be confusing, but reading them in the right-to-left order will be clear once you are more familiar with pointers (and if you programming in C or C++, you want to become familiar with pointers).
If we go back to the initial example (which sends a bunch of warnings on gcc with C99) :
I played with the API, and you can feed it your paths two ways:
or:
Edit
After reading others answers, only two of them (mkb and moshbear) avoid the “just cast the data” error.
In my own answer, I forgot the NULL terminator for the array (but then I don’t know the Linux API, nor the fts_* class of functions, so…)