arguments passed with main (C++)

Started by
8 comments, last by mnansgar 19 years, 5 months ago
you can pass argc and argv to the main function. argc will give the number of arguments, and argv is an array with all arguments were argv[0] is the complete url of the program (like c:\test.exe) But now I saw in an SDL example that they used Nargs and args or something.. Is this just the same? Can I still use the normal argc and argv? Please answer me :P Thanks, Rob
Advertisement
I'm pretty sure you can name those two variables whatever you'de like. However, argc and argv seem to be the most common. Personally, I think it's better to use whatever is most common, to make it easier for other programmers to read your code.
The names of the two variables are completely irrelevant. All that matters is there type and order, that the first one is an int, and the second one is a pointer to a character array, or a pointer to a pointer to a char, or whatever. Name them whatever you want. However, sticking to argc/argv is probably best, since otherwise, you might confuse some people, as you yourself have discovered.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Quote:Original post by Toadhead
you can pass argc and argv to the main function. But now I saw in an SDL example that they used Nargs and args or something..

Is this just the same? Can I still use the normal argc and argv?


In *any* function parameter list, you can name the parameters passed anything you want to, provided that they are the correct types. For instance, one form of the main function requires the following prototype: int main(int, char **). As long as the variables are of type int and char **, you can call them whatever you want to! For instance, int main(int badDay, char **iHad); However, I'd stick with the common nomenclature (argc, argv) -- meaning 'argument count' and 'argument vector' -- since it is sort of a standard among programmers and it actually describes the data being passed.
h20, member of WFG 0 A.D.
owwww
that true, just like all other functions lol.


thanks!



Btw do I need to use this one:
int main(int argc, char* argv)

or do I need to use:
int main(int argc, char** argv)

?
Quote:Original post by Toadhead
int main(int argc, char** argv)


This one. Otherwise you are pointing at a single string.
ow.. yeah but I can also use int main(int argc,char* argv[]) isn't it?
Quote:Original post by Toadhead
ow.. yeah but I can also use int main(int argc,char* argv[]) isn't it?

Yep, in function parameters:

void Function(Type name[])

...is exactly the same as:

void Function(Type* name)

So, yes you can declare main as:

int main(int argc, char* argv[])
Quote:Original post by TheBluMage
Quote:Original post by Toadhead
ow.. yeah but I can also use int main(int argc,char* argv[]) isn't it?

Yep, in function parameters:

void Function(Type name[])

...is exactly the same as:

void Function(Type* name)

So, yes you can declare main as:

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


That's not exactly true; the two case will be treated differently by the compiler, but you are very unlikely to be able to tell the difference. The compiler will store extra information when using an array over a pointer, although you won't have access to it.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Quote:Original post by Toadhead
Btw do I need to use this one:
int main(int argc, char* argv)

or do I need to use:
int main(int argc, char** argv)


First, let's look at what 'argv' represents -- you can think of it conceptually as being a list of arguments which are typically separated by spaces on the command line. For instance, if you call:

gcc -Wall myfile.cpp

Then argv = {"gcc", "-Wall", "myfile.cpp"} (note this isn't the correct syntax, it's more for conceptual value).

OK, so now we know that a char data type represents a single character. If we have an array of chars, then that represents a string of chars. For instance, an array of chars could represent the string "gcc". This is either done by one of the following methods (static vs. dynamic allocation):

char argv[] = "gcc";
char *argv = malloc (strlen("gcc") + 1);

In the second scenario, 4 chars are allocated and the string "gcc" will be copied in later. So we have a problem with your first char* recommendation since it can only store a single string (note that a string is a sequence of ASCII chars which is terminated by a '\0' which is not displayed on the console).

Now, when we have char **argv, the double pointers are a bit confusing, but you can consider it almost the same thing as a two-dimensional array. So in this case, our array's first dimension is a list of strings, and the second dimension is a list of characters in each string. For more information, you should look up double pointers!

Also, you may ask, since we know how many arguments or strings are stored in argv, why don't we just make it a one-dimensional array? The rationale is simple -- because string display routines take the address of the start of the string as their first argument, and why make it difficult to compute when we can automatically make these designations using a double pointer without much additional memory needed?
h20, member of WFG 0 A.D.

This topic is closed to new replies.

Advertisement