1

i am really confused regarding this main function,

int main( int argc, char *argv[] ) {
/*statements*/
}

specifically the

char *argv[ ].

What does that represent exactly? i know that it is a pointer to an array of characters, but how is that array created and how does it work exactly? also is that char array the same as a string, since strings are an array or char?

leppie
  • 115,091
  • 17
  • 196
  • 297
notamathwiz
  • 225
  • 1
  • 5
  • 14
  • 1
    See this: http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean – Shobit Oct 17 '13 at 05:58
  • 1
    `char *argv[]` is not a pointer to array of characters. Instead it is array of 'pointer to char'. In the case of main, array of 'pointer to (first-element of) command line arguments'. Read **[this](http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean)** – smRaj Oct 17 '13 at 06:44

7 Answers7

2

It is a Command line argument. You can just pass some values during execution of the program like below.

#include<stdio.h>
int main(int count,char *argv[]){
    int i=0;
    for(i=0;i<count;i++)
         printf("\n%s",argv[i]);
    return 0;
}

//save file as arg.c

In command line

C:\tc\bin>arg c JS

Output:

*c*
*JS*
Shiva
  • 531
  • 4
  • 6
  • 14
1

It points to the parameters that are passed to your program when you launch it.

Ex: ./a.out toto tata

printf("argv[0]: %s, argv[1]: %s, argv[2]: %s, argv[3]: %s", argv[0], argv[1], argv[2], argv[3]);

Output:

argv[0]: ./a.out , argv[1]: toto, argv[2]: tata, argv[3]: (null)

argc is the number of arguments stored in argv.

You don't have to care about who created it, as it's part of the C standard. Search information about _start function if you really want to know.

argv is an array of string, and each individual string are each selfs arrays of char.

Some time you will see argv noted like this:**argv or argv[][].

Antzi
  • 12,831
  • 7
  • 48
  • 74
  • The last sentence is misleading. argv is **not** a string: each argv item, argv[0], argv[1] is effectively a pointer to a string, but argv itself is not a string, it is effectively an array of pointers, not a single pointer. – djna Oct 17 '13 at 08:45
  • Yep, I was thinking about individual items, but did'nt wrote it properly. Thanks. I edited the question. – Antzi Oct 17 '13 at 08:58
1

char *argv[] is syntactic sugar for char **argv. argv is simply an array of pointers to null-terminated strings. The operating system creates the array for you before invoking your main() function.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

int argc = Number of arguments/parameters when running the program (including program name)

char *argv[] = Arguments as an array of "strings" when running the program. This is how I think of it.

Example:

C:\> echo hello world

argc = 3

argv[0] = echo

argv[1] = hello

argv[2] = world

Matthew
  • 123
  • 1
  • 9
1

It points to the parameters passed by executing the java file. If you have a class called MyClass with a main method, by calling java myclass a b, you will have a and b in this array. Also in c or c++ calling myCommand a b...

Emaborsa
  • 2,360
  • 4
  • 28
  • 50
1

The crucial facts is that it's an array of pointers to characters not a pointer to an array. So you have several pointers to characters, one per "word" in the program's argument list.

djna
  • 54,992
  • 14
  • 74
  • 117
1

char *argv[] is the same as char **argv and argv[0] to argv[argvc-1] are pointers to C style strings(which are NULL terminated). The draft C99 standard actually provides a nice explanation for how it works and what the contents should be, from section 5.1.2.2.1 Program startup paragraph 2 says:

If they are declared, the parameters to the main function shall obey the following constraints:

— The value of argc shall be nonnegative.

— argv[argc] shall be a null pointer.

— If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.

— If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

— The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740