I have a header file & implementation file called by a file containing my main function. Once I build and run my main function, I want the user to call the executable with one argument only.
I know i can use argc & argv, but I feel this is more than what I want (i.e. why should I make the user create and array with one element? just pass the one element-)
QUESTIONS:
1. Is it possible to do what I have?
2. I am on a linux, from cmdline after I have the executable made, ./FWI_mian(ptr), how do i define ptr at the cmdline?
You seem to have a very wrong concept of what exactly argc is.
To pass arguments to a program a user does this: program argument1 argument2 ...
The shell will interpret the command line however it wants and create an array out of that. Assuming program is found, this array will eventually get passed to its main().
The shell will pass at least two things to the program: the command that called the program, and a string with all the arguments. Some shells (Bash) will parse the arguments and pass them directly to the program, while others (Windows) will simply pass the argument string unprocessed and leave interpretation up to the program (in console programs, this happens before main() is called, so don't worry about it). Regardless, you can't restrict how many arguments are passed, except by quitting when the wrong number of arguments is found.
main is an unusual function.
It is called by the operating system, and as C++ can run on many systems and the C++ standards cannot dictate to OS writers what data they pass to programs running on that OS - you
can write any parameters you like in the main functions and the compiler will accept it.
For example:
The following is meaningless but will compile with no problem:
1 2 3 4
int main(char* pc, float g)
{
return 0;
}
IIRC, the C++ standards only stipulate the following concerning parameters to and from main
1. main must return int
2. The follwing prototypes must be honoured by the compiler
1 2
int main(int, char *[])
int main (intchar**)
3. if there is no explicit return statement - then 0 is automatically returned.
Since you only want to pass one argument, what I'm about to say is a bit redundant if applied here, but it would be a good idea in general to have the command line parsing wrapped in a class:
Yes... I was wondering if jsmith would pop up with a link like this... But it was you instead... Anyway, I just wanted to demonstrate a simple implementation for handling switches. I don't doubt that boost does it in a more elegant way (and neither do I doubt that I could do it too if I spent more time on it).