Thread: argv in the global main function and array or pointers to pointers

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658

    argv in the global main function and array or pointers to pointers

    Moderator note: This thread was split from Why do you need pointers to pointers?

    Quote Originally Posted by Salem View Post
    int main ( int argc, char **argv )

    Your first exposure to pointers to pointers.
    It'a an array of pointers to pointers, so this can also be written as

    int main ( int argc, char *argv[] )
    Last edited by laserlight; 05-22-2013 at 08:28 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rcgldr
    It'a an array of pointers to pointers, so this can also be written as

    int main ( int argc, char *argv[] )
    Actually, no, argv is just a pointer to a pointer. The char **argv syntax is equivalent to the char *argv[] syntax, but while the latter hints that an array is involved, that need not be the case (if this were not the global main function).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by laserlight View Post
    Actually, no, argv is just a pointer to a pointer. The char **argv syntax is equivalent to the char *argv[] syntax, but while the latter hints that an array is involved, that need not be the case (if this were not the global main function).
    I was assuming that this was the global main function.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    From context, it must be, but that does not change the fact that argv is a pointer to pointer to char, not "an array of pointers to pointers".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by laserlight View Post
    argv is a pointer to pointer to char, not "an array of pointers to pointers".
    to expand on this a bit, an array is a group of like-types items of a length known at compile time. a pointer is never an array. a pointer may point to the first element of an array, and in fact, a native array decays into a pointer to its first element when passed as an argument.

    this is an array
    Code:
    int foo[10]; // sizeof(foo) = 10 * sizeof(int)
    this is a pointer to the first element of an array.
    Code:
    int *foo = new int[10]; // sizeof(foo) = sizeof(void*)
    this is an array of pointers
    Code:
    int *foo[10]; // sizeof(foo) = 10 * sizeof(void*)
    this is a pointer to the first element of an array of pointers
    Code:
    int **foo = new int*[10]; // sizeof(foo) = sizeof(void*)
    pointers are generally the same size as a long int, but I don't think it's guaranteed to be so. I believe the standard allows for multiple sizes of pointers within an implementation.
    Last edited by Elkvis; 05-21-2013 at 01:52 PM.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by rcgldr View Post

    int main ( int argc, char *argv[] )
    I should have stated that argv is an array of pointers to char, where each pointer is a pointer to the first character of a string. There are (argc) strings, and (argc) pointers in argv[]. argv[0] points to the command that invoked the program (usually the name of the program) in the command line, argv[1] points to the first parameter of the command line, argv[2] points to the second parameter, and argv[argc-1] points to the last parameter. argv[argc] is defined to be NULL in some texts, but I don't know if this is implementation dependent.

    You can find this alternate syntax for main in various web articles and text books. Example links:

    The C Book — Arguments to main

    Main function - Wikipedia, the free encyclopedia

    Post #4 in this thread gives one example of truly using a pointer to a pointer (for Base).
    Last edited by rcgldr; 05-21-2013 at 05:21 PM.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    argv[0] points to the name of the program in the command line
    O_o

    You aren't really wrong, but that also isn't guaranteed.

    [Edit]
    A good example might be command aliases under "Bash" on "Gnu/Linux" systems.

    You'd most likely get the alias instead of the program name.
    [/Edit]

    If you actually need the name of the program as executed, you are much better off reaching beyond the standard library and paraphernalia.

    Soma

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rcgldr View Post
    I should have stated that argv is an array of pointers to char, where each pointer is a pointer to the first character of a string.
    I disagree you should have stated that, since it is wrong. argv is a pointer to pointer. It's value happens to be the address of a pointer to char. That pointer to char has a value equal to the address of the first element of an array of pointers to char.

    Quote Originally Posted by rcgldr View Post
    There are (argc) strings, and (argc) pointers in argv[].
    Wrong. argv is a pointer to a pointer. It points at the first element of an array of argc pointers to char.

    Quote Originally Posted by rcgldr View Post
    argv[0] points to the command that invoked the program (usually the name of the program) in the command line,
    argv[0] points at the first element of an array of char. That array of char is some representation of the name of the program, which is interpreted as a zero-interpreted string. It is implementation defined whether that string is the actual command that invoked the platform (it can also be some symbolic expansion of that command, the name of the executable itself, the full path name of the executable itself, or -- if the implementation cannot populate argv[0] -- a string literal "").

    argc is also allowed to be zero, in which case argv[0] is the NULL pointer.

    Quote Originally Posted by rcgldr View Post
    argv[1] points to the first parameter of the command line, argv[2] points to the second parameter, and argv[argc-1] points to the last parameter. argv[argc] is defined to be NULL in some texts, but I don't know if this is implementation dependent.
    If argc is greater than 1, the contents of argv[1] through to argv[argc-1] are implementation defined. It is a common convention they are related to command line parameters - bear in mind that not all programs are invoked from a command line. The standard mandates that argv[argc] is the NULL pointer.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by rcgldr View Post
    I should have stated that argv is an array of pointers to char

    You can find this alternate syntax for main in various web articles and text books. Example links:

    The C Book — Arguments to main

    Main function - Wikipedia, the free encyclopedia
    Quote Originally Posted by grumpy View Post
    argv is a pointer to pointer.
    There are web articles and text books that define argv as an array of pointers. I included links to two of these in my previous post. argv is an acronym for "argument vector", where vector means array. So your opinion is the people that wrote text books and articles stating that argv is an array of pointers are all wrong?

    char **argv can be considered as the alternate syntax, in the same manner than an array of integers can be declared as a pointer to integer when passed as a parameter to a function.
    Last edited by rcgldr; 05-22-2013 at 09:45 AM.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    in C++, an array's size is known at compile time. if that statement isn't true when applied to a particular object, it's not an array. it's really that simple. when we say "pointer to an array," it simply means that the programmer knows that there are a known number of elements beginning where the pointer points. the compiler has no knowledge of this. it only knows about a pointer to one element.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rcgldr View Post
    There are web articles and text books that define argv as an array of pointers. I included links to two of these in my previous post. argv is an acronym for "argument vector", where vector means array. So your opinion is the people that wrote text books and articles stating that argv is an array of pointers are all wrong?
    Whatever argv "means" can be argued, but it has nothing to do with its type.
    Furthermore, Wikipedia means "argument vector," but never that argc is an array or vector of pointers.
    It is a fact that argv is a pointer to a pointer.

    char **argv can be considered as the alternate syntax, in the same manner than an array of integers can be declared as a pointer to integer when passed as a parameter to a function.
    Precisely so. Therefore, in the function signature, char ** argv and char * argv[] mean the exact same thing: pointer to pointer. The [] are only syntax sugar and really does not mean an array, one of the potential pitfalls of C (which C++ unfortunately inherited).
    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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rcgldr
    There are web articles and text books that define argv as an array of pointers. I included links to two of these in my previous post. argv is an acronym for "argument vector", where vector means array. So your opinion is the people that wrote text books and articles stating that argv is an array of pointers are all wrong?
    Yes, they are all wrong, if they phrased it in that way. The formal parameter conventionally named argv is not an array of pointers: argv is a pointer to a pointer, and a pointer to a pointer is not an array of pointers, even though an array of pointers may be converted to a pointer to a pointer. However, if we were to imagine that the global main function is called, then the actual argument that corresponds to argv could be an array of pointers. Consequently, your implied claim in post #15 that this is not "truly using a pointer to a pointer" is bogus.

    Quote Originally Posted by rcgldr
    char **argv can be considered as the alternate syntax, in the same manner than an array of integers can be declared as a pointer to integer when passed as a parameter to a function.
    This seems to indicate that you think that in this declaration, x is an array:
    Code:
    void foo(int x[]);
    That is not so: x is a pointer to int. The declaration is equivalent to this in every way except spelling:
    Code:
    void foo(int* x);
    So, which is the "alternative syntax"? I don't think there is one: it is just a matter of style. One might prefer the former to hint that an array is expected as the actual argument. On the other hand, one might prefer the latter since the formal parameter is indeed a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Since this discussion about the parameters of main() seems to be a bit off topic, perhaps it should be moved into it's own thread?

    Quote Originally Posted by rcgldr
    There are web articles and text books ... that define argv as an array of pointers such as this one:

    The C Book — Arguments to main

    and state that argv is an acronym for "argument vector" such as this one:

    Main function - Wikipedia, the free encyclopedia

    So your opinion is the people that wrote text books and articles stating that argv is an array of pointers are all wrong?
    Quote Originally Posted by laserlight View Post
    Yes, they are all wrong, if they phrased it in that way.
    That's the way it's phrased in the first article I mentioned:

    The C Book — Arguments to main

    Quote Originally Posted by laserlight View Post
    The formal parameter conventionally named argv is not an array of pointers.
    I've never seen a text book or article that specifically states that the argv parameter for main() is not an array of pointers as opposed to it can be optionally declared in main as a pointer to a pointer. How argv is declared in main doesn't matter, what determines argv's actual structure (array of pointers or pointer to pointer) is the the code that creates argv and then calls main, and all the text books and articles I've read that state how argv is created, state that it's is created as an array of pointers to the command string and parameter strings.

    Quote Originally Posted by laserlight View Post
    This seems to indicate that you think that in this declaration, x is an array:
    Code:
    void foo(int x[]);
    x could be an array or x could be a pointer. This can't be determined by it's usage as a parameter in a function call or prototype. Only the actual instance of the declaration of x would determine if it's an array such as

    int x[256];

    or a pointer such as

    int * x;
    Last edited by rcgldr; 05-22-2013 at 03:56 PM.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    [Edit]
    How argv is declared in main doesn't matter, what determines argv's actual structure (array of pointers or pointer to pointer) is the the code that creates argv and then calls main, and all the text books and articles I've read that state how argv is created, state that it's is created as an array of pointers to the command string and parameter strings.
    I could not help but also address this comment.

    The type of `argv' as either `char ** argv' or `char *argv[]' from the parameter is still a pointer to a pointer. The underlying type, "actual structure", does not matter in any way. You, as in the `main' function, don't own the underlying variable; you get passed a pointer to a pointer: the address of the first element of an array of pointers which themselves point to null-terminated arrays of `char'.
    [/Edit]

    This can't be determined by it's usage as a parameter in a function call or prototype.
    O_o

    That's the only place it can be determined, and that `x' in the relevant prototype is always a pointer.

    I don't know why you don't believe us; this isn't nearly as complicated or conditional as you are making it.

    Soma

    Code:
    void foo(int x[]){} // `x' is a pointer.
    
    int main()
    {
    int x[256];
    foo(x);
    }
    Code:
    void foo(int * x){} // `x' is a pointer.
    
    int main()
    {
    int x[256];
    foo(x);
    }
    Code:
    void foo(int x[]){} // `x' is a pointer.
    
    int main()
    {
    int * x;
    foo(x);
    }
    Code:
    void foo(int * x){} // `x' is a pointer.
    
    int main()
    {
    int * x;
    foo(x);
    }
    Last edited by phantomotap; 05-22-2013 at 04:07 PM.

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Since this discussion about the parameters of main() seems to be a bit off topic, perhaps it should be moved into it's own thread?


    Quote Originally Posted by phantomotap View Post
    I don't know why you don't believe us; this isn't nearly as complicated or conditional as you are making it.
    It's not me that you need to convince, it's the authors of text books and articles such as this one. Did you read this article?

    The C Book — Arguments to main

    Wiki's standard is that some citable reference should be given for any claim. Once again, I have yet to read any text book or article that specifically states that argv in main is not an array of pointers (as opposed to it can alternately declared as a pointer to pointer).

    This is seems to be an issue of semantics. I think it's more helpful to someone new to C / C++ to explain how the actual instance of argv is created, as opposed to how it can be declared as a parameter to main(). Apparently the people that write the text books and articles I mentioned share the same opinion.
    Last edited by rcgldr; 05-22-2013 at 04:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of pointers and pointer for arrays with argv
    By thames in forum C Programming
    Replies: 4
    Last Post: 12-02-2012, 10:24 AM
  2. Global Access to Global Class without so many pointers
    By parad0x13 in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2009, 02:48 PM
  3. Replies: 4
    Last Post: 07-01-2009, 01:53 PM
  4. Array of function pointers
    By Yasir_Malik in forum C Programming
    Replies: 5
    Last Post: 11-20-2003, 08:41 PM
  5. Array of function pointers?
    By The V. in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2001, 08:37 PM