Thread: char *const ?????!!

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    char *const ?????!!

    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);

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    "char *const argv[]" means?? is it similar to "char const *argv[]" ?
    No they are totally different. Let me try

    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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  6. #6
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    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.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is correct, though I'd rather it be:

    const char* ptr
    char* const ptr

    Especially the const part should be to the left - because it's much easier to distinguish the above rather than

    char const* ptr
    char* const ptr

    I think you'll agree.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by Elysia View Post
    That is correct, though I'd rather it be:

    const char* ptr
    char* const ptr

    Especially the const part should be to the left - because it's much easier to distinguish the above rather than

    char const* ptr
    char* const ptr

    I think you'll agree.
    Yeah that is much better to read!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM