argv[0]
in my c++ program shows :/Users/myname/Library/Developer/Xcode/DerivedData/project4/Build/Products/Debug/project4
Instead of the first argument I enter to it. Can I change this?
argv[0]
in my c++ program shows :/Users/myname/Library/Developer/Xcode/DerivedData/project4/Build/Products/Debug/project4
Instead of the first argument I enter to it. Can I change this?
The C programming system RELIES on argv[0]
containing the name of the program itself (with or without path, depending on the system you use). Many programs depend on that to find the path where the program lives, for example.
Your first argument will be in argv[1]
, and the last one at argv[argc-1]
.
In other words:
/usr/bin/myprog foo bar baz
will have
argc = 4
argv[0] = /usr/bin/myprog
argv[1] = foo
argv[2] = bar
argv[3] = baz
If this wasn't followed, all standard programs would go wrong.
Yes, you can :-) Use this:
argv[0] = argv[1];
And you will get first argument in argv[0]
.
But don't do this in real programs, please.