arguments to main( )

Hello there,

can someone tell me why I need the '*' operator in the argument list of main ()?
Does that mean argv stores pointers that point on char variables?

How would I start my program from the command line?

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


Regards!
its an array of c-strings.
as a c-string is an array (or pointer to multiple) of char, it is effectively a 2-d array of chars, one dimension is which string it is, and the other dimension is the array of characters that make up the string.
1
2
3
4
5
   
  012
0 abc
1 def
2 ghi


in windows, you can drag and drop onto a program and that is treated as commandline args.
you can also call it as shown below.
Last edited on
https://www.geeksforgeeks.org/command-line-arguments-in-c-cpp/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Name of program mainreturn.cpp 
#include <iostream> 
using namespace std; 
  
int main(int argc, char** argv) 
{ 
    cout << "You have entered " << argc 
         << " arguments:" << "\n"; 
  
    for (int i = 0; i < argc; ++i) 
        cout << argv[i] << "\n"; 
  
    return 0; 
}


$ g++ mainreturn.cpp -o main 
$ ./main geeks for geeks
Output:

You have entered 4 arguments:
./main
geeks
for
geeks
Last edited on
Thanks guys, sorry for the late reaction.

Two questions @Ganado:

1) Why do you write ./main instead of simply main to call the program?

2) Does it make a difference if I write int main(int argc, char** argv) or int main(int argc, char* argv) ?
I assume they are the same. char* argv[ ] is an array of C-strings. A C-string is a pointer on the first char of a string, so an array of C-strings should be the same as an ... yeah, how would one call these double pointer structure **argv[ ] in words?


1) In Linux bash to run a executable file we write ./ before its name.
see this link

https://unix.stackexchange.com/questions/4430/why-do-we-use-to-execute-a-file
2)

gcc output.

1
2
 prog.c:5:5: warning: second argument of ‘main’ should be ‘char **’ [-Wmain]
 int main(int argc, char* argv){


for more explanation read this

https://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean
(1)
That's just how you'd call it in unix/linux world, which I guess the example I linked was based off of.
Don't worry too much about that, it's equivalent to just typing "my_program my args" or "my_program.exe my args" in Windows.
https://unix.stackexchange.com/questions/4430/why-do-we-use-to-execute-a-file

Edit: Ha, eec beat me.

(2)
Does it make a difference if I write int main(int argc, char** argv) or int main(int argc, char* argv)
The first one is valid, the second is not.

In C++, a function having a parameter of T* var is the same thing as saying T var[], because an array degrades into a pointer when passed to a function.
Therefore, char* argv[] and char** argv are the same thing when talking about function parameters, and are both valid for command-line arguments in main's signature.

char* argv[] is saying it's an array of c-strings. char** argv is equivalent in this case, it's saying the same thing, but you can think of it as saying a pointer to a c-string.

Edit: As jonnin says below, don't worry too much about the syntax. Just know that that is the syntax used for command-line arguments.
What matters more is actually accessing the command-line arguments, which is just saying that each argv[i] is a null-terminated c-string.
Last edited on
./ is used because the OS wants the full path to executable files. ./ is the current folder, in unix command line speak. (useful: .. is up one folder, so ../program is in the folder above your current)
you can edit one of your files, I think it is your .profile (?) (its been many, many years for me here) to make the ./ the default making it easier and more dos-like to run programs (the ./ goes away and you can just type the program's name). 100 years ago (at least it feels like it today) when I was doing this it was standard practice to do that; it may be considered a security problem or something now, I really don't know.

Windows works differently; it searches the current folder first, and if its not there, it searches the windows path, and if its not there, it errors out. So just typing programname works if you are in the folder where that resides.

in words, its exactly what I said the first time, it is an array of strings. That is all you need to know to understand it conceptually; the funky pointer and array syntax -- you will pick that up with practice, for now, don't sweat it too much. The above post gives you the technical in-words...
Last edited on
1. Ganado types commands on some shell.
One example of command:
g++ mainreturn.cpp -o foo

There the g++ is a command and mainreturn.cpp -o foo are arguments for it.
The shell must find the command g++. It looks from a list of directories for a file with that name.
Presumably (for security reasons) the list (aka path) does not contain the current directory (.).

Another example:
foo geeks for geeks

None of the directories in the path contains executable named foo. Command not found.
./foo geeks for geeks

Explicit name, the foo that is in current directory. Shell does not seek from the path.

2. int foo( T * bar ); and int foo( T bar[] ); are identical.
The int foo( T bar ); is not.

char** argv
The argv is a pointer to a pointer. That is certain.
In practice argv is a pointer to an array of pointers.

Each pointer in the array is pointing to an array of char. The last character on those arrays is null.

C-string is not a pointer. C-string is an array of char that has a null as terminating character.


The argv is a list of words?
$ ./main "geeks for geeks"
Output:

You have entered 2 arguments:
./main
geeks for geeks
Last edited on
Thank you guys.

I think switching to Linux in the long run is a good idea...

Ahh, so it's either char** argv or char* argv[ ]
This makes sense, also due to keskiverto's point 2.).

Thanks, great community here!
It ends up being about the same for most programmers, I would think. That is, Ive dumped cygwin on my windows box, so my dos prompt has all the unix shell commands like grep in it now, and even g++ and make stuff if I want to go there. Or you can run visual studio over on a unix box. The lines tend to blur for programmers who know how to get the best of both worlds on any type of box. Move if you want, but don't feel compelled to do so.
I agree with jonnin. Being familiar with both Windows and Unix/Linux is important, but don't feel like you need to pick one in the "long term". Honestly, even cygwin is overkill for me. A simple MSYS2 console for compiling libraries Linux-style (make) for Windows has worked pretty well for me.* There are also Windows versions of tools like grep (see: http://gnuwin32.sourceforge.net/packages/grep.htm)

* https://github.com/msys2/msys2/wiki/How-does-MSYS2-differ-from-Cygwin
(msys2 and cygwin have different goals, I'm just talking about making development on Windows as seamless as possible through msys)
Last edited on
Topic archived. No new replies allowed.