Thread: int main ( int argc, char **argv )

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    int main ( int argc, char **argv )

    Hi,

    Code:
    int main ( int argc, char **argv ){}
    int main ( int argc, char *argv ) {}
    An array of char pointers can be represented as a function parameter one of two ways: char *name[] or char **name. I have chosen to use the latter, you can use either one, it is a matter of style, which I refuse to dictate
    This is quoted from FAQ : http://faq.cprogramming.com/cgi-bin/...&id=1073086407

    But, I am still confused between this and: http://www.cprogramming.com/tutorial/c/lesson14.html

    Can anyone explain that simply for me?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In the code you quoted, the former is correct, the latter is wrong. (But since this is C, both main functions should return 0.)

    Under certain circumstances, an array decays to a pointer to its first element. In that way, char **argv and char *argv[] are equivalent.
    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
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    char *argv .. u r sending a pointer to a char as an argument..this can be a normal pointer or point to a array of chars...


    char **argv .. u r sending a pointer to an array of pointers as an argument . which means, each element of this array can itself point to a char array..

    i hope this is clear enough

    and as u mentioned char **argv or char *argv[] are both the same

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    char *argv ...
    This is wrong. Function prototypes must match exactly. laserlight nailed it.

  5. #5
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    char *argv is wrong....i didnt say it was right..
    i just explained the diff between char *argv and char **argv.. was that wrong too??

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Nowhere in your statement did you tell the original poster that char *argv was not a valid parameter of main, so he might try to use it like that. Not to mention that you're posting to a thread that was answered clearly some three hours ago.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Thanks all it is clear now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM