LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   main(int argc, char **argv) (https://www.linuxquestions.org/questions/programming-9/main-int-argc-char-%2A%2Aargv-192598/)

Longinus 06-12-2004 01:20 AM

main(int argc, char **argv)
 
in main(int argc, char **argv)

what does the char **argv do? why the double asterisks?

Dark_Helmet 06-12-2004 01:44 AM

I assume you're familiar with pointers and their general use... otherwise this stuff won't make any sense. If you need a crash course in pointers, I can give a (reasonably) quick one...


The char ** syntax can be a little confusing. I've seen some people use this syntax:

int main( int argc, char *argv[] )

To me, that's a little more descriptive (although functionally equivalent). As that format suggests, argv is an array of strings; an array of char *. Each argument on the command line is given as a separate string. So, for instance, to access the first argument (which happens to be the program name), you would use argv[0]. To get the first "real" argument/option, use argv[1], and so on.

In other words, the command line has been fairly neatly parsed for you to start with rather than just dumping the whole thing into one (potentially) behemoth string.

Make sense?

Longinus 06-12-2004 01:57 AM

ah ok so char **argv is a shortcut to make a pointer to an array of something

right?

ashwinipahuja 06-12-2004 04:13 AM

plz revise ur concepts about pointers . refer some good books . may be ritchie or schaum series . it is on pointers

Dark_Helmet 06-12-2004 07:22 AM

Any introductory C book will discuss pointers. If it doesn't, it's not worth the paper it's printed on.

A "pointer" is a variable that stores the address of something in memory. You specify exactly what it points to by giving the data type, and then specify it's a pointer by using a *. In this case, a "char *" means that the value is the address in memory of a "char" variable. A char is a type of variable typically used to represent a letter. In C, a "string" is a collection of char values. Each char in the string is stored in consecutive memory locations. The end of the string issignified when the special character (NULL) is encountered. NULL has a value of exactly 0. Thus, in memory, the string "pointers_are_fun" would look like:
Code:

p o i n t e r s _ a r e _ f u n 0
To use the string in the program (for printing, analysis, or whatever), you must refer to it by the starting address of the string. That is what a "char *" is if you recall: a memory location containing one or more characters.

So if you expand on that idea, a "char **" is the location in memory, of one or more strings. Think of it like the "pointers_are_fun" example earlier. Except, instead of letters, you have "char *" values stored in sequence. In other words, you have a collection of addresses that each point to a string of characters. You have to make two "hops" to get to a given string: Starting at "char **", pick out one of string you want to examine, then use that value as a string. It's a little hard to describe. Perhaps someone else will add a little more to help clarify it.

Like I said, any introductory C text ought to explain this in much greater detail. Pointers and strings are fundamental parts of C programming. To leave them out would be criminal.


All times are GMT -5. The time now is 12:00 PM.