Thread: Question on *argv vs. *argv[]

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    Question on *argv vs. *argv[]

    Hi. Just wondering. Sometimes I see source code where
    the *argv[] in:

    ex1
    Code:
    int main(int argc, char *argv[])
    is declared as

    ex2
    Code:
    int main(int argc, char *argv)
    I know that the ex1 is a pointer to an
    array of pointers, and ex2 is a pointer to
    char, but what difference is there in a C program
    when the declaration is like example 2 opposed to
    example 1?

    thanks.
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    More likely you saw:
    Code:
    int main(int argc, char **argv)
    In which case it is pretty much the same thing since an array decays to a pointer to its first element.
    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
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    thank you laserlight. Yes, maybe that was what I saw.
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    technically, C distinguishes arrays from pointers (arrays have certain metadata such as sizeof an array will give you the number of its elements times the size of each element, whereas sizeof a pointer will give you just the size of that pointer). However when passed as function arguments array's lose their "arrayness" and become virtually indistinguishable from pointers. That being said, I prefer ex1 because it allows you to visualize the concept of "an array of strings" much better. Also, there's two more you may also encounter:
    Code:
     int main (void)
    int main (int argc, char *argv[], char *env[])
    Oh, and how could I forget...
    Code:
    void main ()
    (joking, never use that one)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM