LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 06-12-2004, 01:20 AM   #1
Longinus
Member
 
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
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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
Longinus
Member
 
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

right?
 
Old 06-12-2004, 04:13 AM   #4
ashwinipahuja
Member
 
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
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

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


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:31 AM.

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