My question is how to pass int
arguments through argv[]
.
The form I want to use is the following: while the NTHREADS, and LETTER can be optional, the FILE1 must be provided.
./test [-t NTHREADS] [-l LETTER] FILE1
How can I handle this?
My question is how to pass int
arguments through argv[]
.
The form I want to use is the following: while the NTHREADS, and LETTER can be optional, the FILE1 must be provided.
./test [-t NTHREADS] [-l LETTER] FILE1
How can I handle this?
You can't. All arguments to main()
are char*
by definition. If you want you can convert them, but that's done at your own discretion. There are tools like getopt
that make writing interfaces like this a lot easier, and they can be used to do conversion as necessary.
You can use either the atoi() function, which is available in the stdlib.h standard library, or for a more robust solution you can use strtol(), which is also in stdlib.h
Example:
#include <stdio.h>
#include <stdlib.h>
int main( int numArgs, char **argList )
{
int num, i;
for( i = 0; i < numArgs; i++ )
{
fprintf( stdout, "\nArgument #%i (\"$s\") is %li as a number.", i, argList[i], strtol(argList[i], NULL, 10) );
}
return 0;
}