Hi Guys,
I am trying to get used to pointers. Please help me understand that in below code what does "char *const argv[]" means?? is it similar to "char const *argv[]" ?
Code:int getopt(int argc, char *const argv[], const char *optstring);
Hi Guys,
I am trying to get used to pointers. Please help me understand that in below code what does "char *const argv[]" means?? is it similar to "char const *argv[]" ?
Code:int getopt(int argc, char *const argv[], const char *optstring);
No they are totally different. Let me try"char *const argv[]" means?? is it similar to "char const *argv[]" ?
char *const argv[] ==> char pointer to a const char array
char const *argv[] ==> const array of char pointers to a non-constant char.
I might me wrong as well. But what that arguments saying is that what ever value which was sent by the caller function cannot be chnages within the getopt function.
ssharish
Isn't it the other way around?
char* const --> Constant pointer to char* array (you can't change where the pointer points, because the pointer itself is constant!).
const char* or char const* --> Pointer to a const char* array (you can't change the values it's pointing to, because the memory itself where it's pointing to it const).
Basically, everything before the * is considered the type of the pointer.
But if the const is after the *, it's the actual pointer itself that's const.
And technically, it probably should be considered "const char * const argv[]" (or "char const * const argv[]"), as it is probably not really "pure" to modify the argv parameters them selves - you don't know how they are created or how they are stored, so it's unsafe to make any modifications to them - although I've seen more than one case of them being modified "in situ". You certainly don't see them as const very often.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
argv, as given to the canonical main, is char** (or char *[], but that's just an alternate way of writing it, and in this case more confusing).
getopt takes it as char *const *. This means it promises it won't change the pointers in the array, i.e. won't make one of the pointers point to something else. It doesn't promise that it won't change the strings pointed to.
The reason for this, however, is not that it wants to modify the strings. It's for language reasons: you cannot assign a char ** to a char const ** - the automatic conversion to const only works for the outermost pointer level. Thus, the interface of getopt() is easier to use if the argument type is char *const* instead of char const *const *.
By the way, glibc's implementation lies. It actually does change what the pointers point to, because it permutes the values in the array. Unless the option string contains + as the first character, or the environment variable POSIXLY_CORRECT is set. They get away with this behaviour because no program ever passes anything but the real argv as an argument to the function.
All that said, I consider getopt() to have one of the most horrible interfaces ever. If you're using C++, you should use Boost.program_options instead.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
so.. if i am understanding correct:
char const *ptr means that we cannot change the value of *ptr i.e value pointed by ptr.
char *const *ptr means we cannot change the value of ptr. i.e address contained in ptr.