Strawberry
Strawberry

Reputation: 67968

What is this asterisk for?

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[])

Upvotes: 2

Views: 1532

Answers (6)

Kartikya
Kartikya

Reputation: 453

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 :)

Upvotes: 0

Amokrane Chentir
Amokrane Chentir

Reputation: 30405

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!

Upvotes: 0

asciz
asciz

Reputation: 125

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[].

Upvotes: 5

John Bode
John Bode

Reputation: 123578

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.

Upvotes: 2

Jonathan Wood
Jonathan Wood

Reputation: 67345

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

Upvotes: 0

duffymo
duffymo

Reputation: 309008

It means that argv is an array of character pointers.

Upvotes: 2

Related Questions