Thread: what's the mean of : char* const* argv ?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    13

    what's the mean of : char* const* argv ?

    int command(int argc, const char*const* argv);

    in this line
    what's the mean of : char* const* argv ?

    i don't understand it

    Thank you for explaining it for me!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The parameter argv is meant to be an array of strings.

    There is a reason for the const modifier, as the argv pointer wont be reassigned, the elements will have to be accessed by subscript. This makes it safe for the caller to dynamically allocate argv, pass it into command(), and free it later. Otherwise, if you point a pointer elsewhere before it is freed, then you've leaked the memory that it used to point to.

    So for example an expression like this

    argv[counter]

    is OK, but an expression like this

    *argv++

    is not.

    Of course, you can choose not to use const and just use the subscript notation, but the compiler will not enforce its use.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    char* const* argv creates two levels of indirection - the first level is a constant pointer to char, and the second level is a pointer to the constant pointer.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, const char * is the string part, and const *argv would be the dynamically allocated array part.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by whiteflags View Post
    Yes, const char * is the string part, and const *argv would be the dynamically allocated array part.
    Whoops! and thanks for pointing that out whiteflags. Need to patch my post as I totally missed the first const in the declaration.
    const char* const* argv creates two levels of indirection - first level is a const pointer to a const char, and second level is a pointer to the const pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argc, argv
    By Martas in forum C Programming
    Replies: 6
    Last Post: 11-19-2009, 09:39 AM
  2. argv
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-14-2007, 08:57 AM
  3. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  4. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM