0

This is probably a lot more simple than I am making it, but I am having trouble parsing a command line argument that is not given a size argument (i.e. I'm asking for int main(char* argv[]) rather than using int main(int argc, char* argv[]) ). I suppose this is really more of a two part question, but I was wondering (1) can you run a program with command line arguments that look like the following?:

testProgram arg1 arg2 arg3

where arg1, arg2, and arg3 would be placed into argv[], and (2) if you can, how do you cycle through each argument so that you might be able to use them within the program?

I know this probably seems very arbitrary, but I'm trying to understand better how command line arguments work and how to use them properly.

Thanks for the help and insight.

2
  • Have you tried that? Commented Sep 22, 2014 at 0:30
  • @0x499602D2 sorry, I'm making an assumption, but do you mean have i tried something like testProgram arg1 arg2 arg3? If so, then yes, and whenever I try to do a simple cout of the first argument, I get something like testProgram: arg2: binary operator expected Commented Sep 22, 2014 at 0:44

3 Answers 3

4

The signature

int main(char* argv[])

is not supported by any C++ compiler I know of. Admittedly very few nowadays. But still.


The signature

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

is standard and works well for pure ASCII arguments.

It also works well for general Unicode arguments in Unix-land, when they're encoded as UTF-8.

Anyway, it tells you the number of arguments.

Sign up to request clarification or add additional context in comments.

Comments

1

argc tells you how many arguments the program received. So argv[0] refers to the name of the program itself, argv[1] to the first argument you passed on the command line, argv[2] to the next, and so on up to argv[argc-1], which is the last one. That's followed by argv[argc], which is a null pointer.

So, when you run the program, you don't have to specify the number of arguments--you just pass whatever number of arguments you want to. Some code in the library then goes through, splits what you pass up into individual arguments, and tells the program how many it found.

IOW, when you run:

testProgram arg1 arg2 arg3

your program is going to receive:

argv[0] = testProgram
argv[1] = arg1
argv[2] = arg2
argv[3] = arg3
argv[4] = null pointer
argc = 4

Note: the exact content of argv[0] is open to variation--it might be exactly what you typed to invoke the command, or it might be a full path to the command, or it might (especially on ancient systems) be an empty string. On most modern systems, it'll normally contain something like the file name though.

1 Comment

argv[0] being a null pointer is also possible (with argc being 0)
0

You can ignore the argc argument and process the argv[] elements until you get to the one that's NULL since the argv array is specified to have a sentinel element that's NULL. I'm not sure what the ultimate point of the question is though.

#include <stdio.h>

int main(int argc, char** argv) 
{
    while (*argv) {
        puts(*argv);
        ++argv;
    }
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.