-3

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?

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92

2 Answers2

2

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

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.

vladon
  • 8,158
  • 2
  • 47
  • 91