2

I'm learning c programming, and I don't understand what is this asterisk for in the main method.

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

Strawberry
  • 66,024
  • 56
  • 149
  • 197
  • http://stackoverflow.com/questions/162941/why-use-pointers – erisco Feb 15 '11 at 02:04
  • 2
    Well, what does your book say about it? – GManNickG Feb 15 '11 at 02:09
  • Check out http://www2.research.att.com/~bs/bs_faq2.html#whitespace as well. Good explanation on the meaning, as well as comments on variations you may see due to coding styles. – gr0v3r Feb 15 '11 at 02:09
  • @Gman - We don't have a book. – Strawberry Feb 15 '11 at 02:10
  • 3
    @The Scrum Meister - LMGTFY links are discouraged for being extremely condescending. I might agree that this is a silly question, but that doesn't mean you can be a dick to the asker. – Chris Lutz Feb 15 '11 at 02:12
  • 4
    @chris-lutz For a new user - agreed. To a user that asked 206 Questions, **and should have read the FAQ** - not agreed. – The Scrum Meister Feb 15 '11 at 02:19
  • @The Scrum Meister - No. You shouldn't be a dick to anyone, regardless of how they act. (You _can_ be a dick to some people and most people wouldn't tell you not to, but I still don't think you _should_.) If you think the user is a problem (and I wouldn't necessarily disagree) you could bring it up on Meta. I think the great rep-readjustment was intended to solve this sort of thing, so I don't know if it's as big of a problem as it used to be, but it seems to still be. – Chris Lutz Feb 15 '11 at 02:24
  • 1
    @chris-lutz Look, when new user's ask such questions - nore *puzzling* *interesting* or *problematic*, rather questions that can easily answered by searching the web, Usually i direct the user to the FAQ's since probably the user doesn't know what SO is about. But it is user who asked many "What is ..." questions, it's wrong for you to take a shot at me for posting a LMGTFY link.. – The Scrum Meister Feb 15 '11 at 02:33
  • 1
    @TheScrum: It does seem like a simple question, but how are you supposed to search for it if you don't already *know* that it's called a pointer? That's a problem with a lot of syntax questions. It's very difficult to search for `*` in C, or `!` in some other language. – Cody Gray - on strike Feb 15 '11 at 05:16
  • @Cody Searching for "c asterisk" gives results for pointers.. – The Scrum Meister Feb 15 '11 at 05:30
  • @TheScrum: **The top 3-4 results when I search for that are questions on Stack Overflow.** The only reason you get results for pointers is because someone has already asked a "stupid" or "silly" question like this one. I agree this is a duplicate, but that's an entirely different problem than it being a bad question. – Cody Gray - on strike Feb 15 '11 at 05:31
  • @cody Questions regarding **basic** syntax research on a language you are learning, in my opinion do not belong on SO. Judging by the responses i received to my meta question, i am definitely wrong. – The Scrum Meister Feb 15 '11 at 05:37
  • @TheScrum Questions about programming languages don't belong on a site devoted to answering questions about programming? The logical leap there is staggering. – Em McDonald Feb 15 '11 at 07:02
  • 1
    @mark-mcdonald Please don't misquote me. Did you not see the bold word? If i were to ask a question "I am learning html, What is the <" Do you think such a question belongs here? – The Scrum Meister Feb 15 '11 at 07:17
  • "You shouldn't be a dick to anyone" -- Indeed *you* shouldn't, and yet ... – Jim Balter Feb 15 '11 at 11:55
  • 1
    "I'm learning c programming" -- You're learning it how? "We don't have a book" -- so what do you have ... and why not? I advise getting yourself a good primer; it's likely to be a much more effective approach than your current one. – Jim Balter Feb 15 '11 at 12:00
  • @The Scrum Meister No, I saw it. "Basic" is a rather subjective quality, totally dependent on one's experience and knowledge. It's not your (nor mine) place to be the arbiter of what qualifies as sufficiently sophisticated question. This isn't a club, there's no minimum knowledge required to participate. just a desire to learn and/or inform. – Em McDonald Feb 17 '11 at 15:32

6 Answers6

5

char* a; means that a is a pointer to variable of type char.

In your case argv is a pointer to a pointer (or even several of them - it is specified in argv in your case) to a variable(s) of type char. In other words, it's a pointer to an array (of length argv) of pointers to char variables.

You can even write your code this way: int main(int argc, char** argv) and nothing, actually, changes as soon as char* a is the same as char a[].

asciz
  • 125
  • 1
  • 4
  • 1
    char* a is not generally the same as char a[]; it only is so in a parameter declaration. A pointer to a pointer to char *is not* "in other words" a pointer to an array; these are quite different beasts. But in *parameter* context, and only in parameter context, a[] is identical to *a. – Jim Balter Feb 15 '11 at 12:06
  • @jim-balter sure, i just didn't want to put "in this case" too often. this clarification was need here indeed. – asciz Feb 15 '11 at 12:32
2

It means that argv is an array of character pointers.

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

The declaration char *argv[] declares argv as an array (of unknown size) of pointer to char.

For any type T, the declaration

T *p;

declares p as a pointer to T. Note that the * is bound to the identifier, not the type; in the declaration

T *a, b;

only a is declared as a pointer.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • In parameter context, foo[] *does not* declare an array, it declares a pointer, so char *argv[] is identical to char **argv. – Jim Balter Feb 15 '11 at 12:04
0

It signifies a pointer. char argv[] declares an array of characters. char* argv[] declares an array of character pointers, or pointers to strings.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • As noted in a comment to duffymo's answer, this is incorrect. In parameter context, foo[] does not declare an array, it declares a pointer. As a parameter, char* argv[] is identical to char** argv. – Jim Balter Feb 15 '11 at 12:02
  • @Jim: I'm not sure I see the distinction. Since C treats arrays and pointers the same in many contexts, and here we're talking about a reference to an already-existing array, in what way is it wrong to say the square brackets denote an array? It's true we're not declaring an array, only an address is passed as an argument. But it's a pointer to an array. – Jonathan Wood Feb 15 '11 at 13:17
  • No, it's not a pointer to an array -- if it were, then `sizeof(*argv)` would give the length of the array, rather than the length of a `char*`, and `(*argv)+1` or `&argv[1]` would point to the second array, rather than to the second `char*`. Refer to the C standard for all the differences. – Jim Balter Feb 15 '11 at 13:21
  • P.S. In http://cm.bell-labs.com/cm/cs/who/dmr/chist.html under Critique, Dennis Ritchie writes: "some rules designed to ease early transitions contributed to later confusion. For example, the empty square brackets in the function declaration int f(a) int a[]; { ... } are a living fossil, a remnant of NB's way of declaring a pointer; a is, in this special case only, interpreted in C as a pointer. The notation survived in part for the sake of compatibility, in part under the rationalization that it would allow programmers to communicate to their readers an intent to pass f a pointer ... – Jim Balter Feb 15 '11 at 13:39
  • ... generated from an array, rather than a reference to a single integer. Unfortunately, it serves as much to confuse the learner as to alert the reader." BTW, this turns out to be quite a problem in C++, where arrays *could* be passed by value if the syntax weren't already usurped. – Jim Balter Feb 15 '11 at 13:40
0

Those are parameters passed from the command line to your program. This asterix is a pointer operator.

Basically char argv[] is an array of characters, char *argv[] is a pointer to an array of characters. So it is here to represent multiple strings to put it simply!

Note that: char *argv[] is equivalent to char * * argv, as char argv[] could be represented as char *argv.

Just to go further you would be amazed that those two expressions are equivalent:

int a[5];
int 5[a];

This is because an array of integers is a pointer to a set of integers in memory. So a[1] can be represented as *(a + 1), a[2] as *(a + 2) etc. Which is equivalent to *(1 + a) or *(2 + a).

Anyway, pointers are like one of the most important and difficult notion to grasp when starting programming in C so I would suggest you taking a serious look at it on Google!

gr0v3r
  • 698
  • 5
  • 5
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
0

This " * " over here is, for sure to specify a pointer only, to place the argv[] //variable number of argument values// to a place it can fit.

Cause you don't know how many parameters will the user be passing as it is argc [argument count] and argv [argument value]. But we do want to allocate them a space where they can fit so we use a pointer with no defined specific SIZE, this pointer will automaticaly find and fit to appropriate memory location.

Hope this helped, if this didn't I'll be glad to help just let me know :)

Kartikya
  • 455
  • 2
  • 9
  • Your answers are very hard to interpret. This is at least in part due to poor English. Please take this as constructive criticism. – Jim Balter Feb 15 '11 at 12:08