Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-12-2004, 01:20 AM   #1
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Rep: Reputation: 30
main(int argc, char **argv)

in main(int argc, char **argv)

what does the char **argv do? why the double asterisks?
Old 06-12-2004, 01:44 AM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
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?
Old 06-12-2004, 01:57 AM   #3
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
ah ok so char **argv is a shortcut to make a pointer to an array of something

Old 06-12-2004, 04:13 AM   #4
Registered: Mar 2004
Posts: 79

Rep: Reputation: 15
plz revise ur concepts about pointers . refer some good books . may be ritchie or schaum series . it is on pointers
Old 06-12-2004, 07:22 AM   #5
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
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:
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass argv[] & argc to functions? skie_knite007 Programming 2 05-13-2005 11:12 PM
char to int: a twist Guru3 Programming 5 12-05-2004 08:49 AM
Converting int value to char liguorir Programming 8 05-23-2004 07:21 PM
argc argv linuxanswer Programming 8 10-25-2003 07:54 PM
int, long in char * marek Programming 10 09-01-2003 10:43 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration