Why do some people put main(int argc, char ** argv) not main() ?

What's the difference? When is it needed?

Thanks in advance,
Tom.

The two arguments are so that your program can read the command line. If your program is called MyProgram and it translates phrases from one language to another the command line could be:

MyProgram wordtotranslate engl fre

This has argc=4 (4 parameters : MyProgram wordtotranslate fromlang tolang)

argv is a list of the parameters, argv[0] is the program name, and argb[3] is the language to translate to.

I've done this by memory, check your compiler help file, but more or less this is why there are main parameters.

OH, I totally get it now, thanks.

At first your explanation was confusing, I thought you were referring to actually using console like in cin/cout but you're referring to when you actually run your software from cmd with arguments.

Makes sense.

However, I'm still unsure how the ** works. I've never seen two asterik's next to eachother like that. When can't they just use one *?

Edited 3 Years Ago by tomz6: d

The asterisk in this context always means a pointer to something. It turns out that the "something" can also be a pointer. So, char **argv means that argv is a pointer to a pointer to a char. The reason for this is that each command-line argument is read in as an array of characters. In C/C++ you can refer to an array by using a pointer to the first element. So, when you make an array like this:

int arr[10];

Then arr is a pointer to the first element in the array. The same thinng is true for character arrays:

char word[] = "Hello!";

word is a pointer to the first character in an array of characters. Now, if your wanted to make an array of words:

char words[][] = { "Hello", " ", "World!" };

So words is a pointer to an array of character arrays; it's a pointer to a pointer. If you wanted to pass words to a function, you'd need to use the signature char **words. This is what you see in your main example above.

So I guess it's kinda like saying (literally):

* (char * argv)

Still a bit confusing. I dont get why they couldn't just do something like:
string word[]
and store all the individual words in the word[] array. But I guess it's the same thing?
Since string is a pointer, and word array is a pointer..

:/

Edited 3 Years Ago by tomz6

The string type is not a built in type. Strings are objects made from a class. The only native type that can hold a string is a char array. The standard for c++ says that main must look like one of these three options.

int main();

int main(int argc, char **argv);  // these are the same
int main(int argc, char *argv[]); // these are the same

The standard for c++ says that main must look like one of these three options.

Or equivalent, to account for different names and things like typedef. The following is still strictly conforming:

typedef int foo;
typedef char *bar;

foo main(foo shizzle, bar *nizzle);

The C-style way of defining main with an explicit void parameter is also allowed and equivalent to the empty parameter list:

int main(void);

Edited 3 Years Ago by deceptikon

I don't like the

char **argv

way of defining a list of pointers either, it is confusing to my three neuron brain. But once you realise you can get at the strings like this:

char szOutLang [_MAX_FNAME] ;
strcpy (szOutLang,argv[3]) ; // get hold of the 4th parameter

you can get on with the real work. Doing the argv[3] gives you a

char*

which you can simply copy into variables with more sensible names, as shown above.

i think we use int main(int argc,char**argv)

when we don`t need user interface to input arguments

if you enter arguments by compiler program with this code

#include<stdio.h>
#include<conio.h>

int main(int argc,char **argv)
{
  clrscr();
  printf("\n%d",argc);


  getch();
 return 0;
}

will output number of your arguments which you input

This article has been dead for over six months. Start a new discussion instead.