Question

I have a main function,

int main (int argc, char **argv)
{}

I'm supposed to pass two different command-line arguments:

argv[1]=IP ADDRESS type
argv[2]=INTEGER type

For example, ./a.out IP-ADDRESS INTEGER. I am calling this binary with some other script.

The problem is while argv[1] is not available it behaves like INTEGER value as argv[1].

How can I put a check in that if argv is type INT don't make it to the argv[1] or only IP-ADDRESS type is allow in argv[1]?

Was it helpful?

Solution 2

int main (int argc, char **argv)

is more commonly expressed as:

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

that is, argv is a pointer to an array of chars. What those chars represent is up to you, but generally these are going to be zero-delimited strings when the program is called on the command-line.

If the user does not supply positional parameters then maybe you should be using options. See getopt().

For example:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

/* Options:
       -i : integer argument
       -p : ip address
*/
int main (int argc, char *argv[])
{
    int option;
    int integer = 0;
    char *ip_address = NULL;

    opterr = 0;
    while ( (option=getopt(argc, argv, "i:p:")) != EOF ) {
      switch ( option ) {
        case 'i': integer = atoi(optarg);
                  break;
        case 'p': ip_address = optarg;
                  break;
        case '?': fprintf(stderr,"Unknown option %c\n", optopt);
                  exit(1);
                  break;
      }
    }

    if (integer)    printf("INTEGER: %d\n", integer);
    if (ip_address) printf("IPADDRESS: %s\n", ip_address);

    return 0;
}

Sample runs:

./a.out -i 1234 -p 128.0.0.1
INTEGER: 1234
IPADDRESS: 128.0.0.1

./a.out -i 1234 
INTEGER: 1234

./a.out -p 128.0.0.1
IPADDRESS: 128.0.0.1

./a.out -p 128.0.0.1 -i 1234 
INTEGER: 1234
IPADDRESS: 128.0.0.1

./a.out -x stuff 
Unknown option x

OTHER TIPS

The char **argv variable passed to the main function is not just a array of "arguments", it is a array of char* that only contains null-terminated-strings you typed in your shell.

As an exemple, calling your program with ./a.out 127.0.0.1 12 will make that argv will look as following:

argv[0] = "./a.out\0"

argv[1] = "127.0.0.1\0"

argv[2] = "12\0"

argv[3] = NULL

You need to make some code to transform and stock the strings in argv, in order to handle them the way you want.

Actually, both argv[1] and argv[2] are char arrays (well, pointers towards 0-terminated arrays of char, aka stringz). The only way that could tell you if that stringz represent integers or IP addresses, is to parse them (ie. to convert strings to IP addresses or to integers).

Bear in mind that argv[N] strings are created just as much as needed to store the arguments passed through the command-line invocation. If the user does not provide an IP-ADDRESS argument, then it won't be the arrgv[2] that is missing, but simply argv[1] will become the sole stringz as there is now just a single argument.

The basic idea: they are just strings, and the user can pass ANYTHING he wants in those strings. It is your job to parse the strings and make sure that they are meaningful.

argv is an array of char* with null terminated strings

you can use argc to get the number of arguments passed to your program.

if(argc == 3) // you program name is counted
{
   //if ip is here
}
else if (argc == 2)
{
   //// int is here
}

then you can check if the ip is passed or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top