简体   繁体   中英

Argc returning as 1, argv returning NULL, despite putting in command line arguments

I suppose I'm a little rusty on C command line arguments. I looked at some old code of mine, but no matter what this version keeps segfaulting.

The way this would be ran is ./foo -n num (where num is the number the user inputs in the command line)

But somehow it isn't working. Where do I go wrong?

EDIT: It's segfaulting when I try to access atoi(optarg) which is atoi(0x0) which segfaults.

int main(int argc, char *argv[])
{
    int c;
    int maximum_n = max_n(); /* Stores the maximum value in the sequence a long can hold */
    unsigned long *array = NULL;

    while((c = getopt(argc, argv, ":n:")) != -1) {

        switch(c) {
        case 'n':
            if(atoi(optarg) > maximum_n) {
                printf("n is too large -- overflow will occur\n");
                printf("the largest Fibonacci term than can be calculated is %d\n", maximum_n); 
                exit(EXIT_SUCCESS);
            }
            else {
                array = fib_array(atoi(optarg));
            }
            break;
        }   
    }

    printf("The %d Fibonacci term is %lu\n", atoi(optarg), array[atoi(optarg)]);

    return 0;
}

Like many programming languages, in C argv[0] usually contains the name of the program. The value actually depends on the arguments to the exec* system call that was used to start the process. For your purposes, that part doesn't matter. What is important to note is that argc will always be one greater than the number of arguments your program received on the command line. In your case argc == 3 and the value of argv should look like

argv[0]: "./foo"
argv[1]: "-n"
argv[2]: "10"

The reason your program is segfaulting is you're accessing optarg after it's no longer valid. The value of optarg can change every time you call getopt() . When you call it outside of the while loop, it has been changed to NULL . You should store the value of atoi(optarg) to a variable while you're in the switch case for 'n' .

Since you're still confused about it, I've updated your code with the necessary fixes.

int main(int argc, char *argv[])
{
    int c;
    int maximum_n = max_n(); /* Stores the maximum value in the sequence a long can hold */
    unsigned long *array = NULL;
    int n = -1;

    while((c = getopt(argc, argv, ":n:")) != -1) {

        switch(c) {
        case 'n':
            n = atoi(optarg);
            if(n > maximum_n) {
                printf("n is too large -- overflow will occur\n");
                printf("the largest Fibonacci term than can be calculated is %d\n", maximum_n); 
                exit(EXIT_SUCCESS);
            }
            else {
                array = fib_array(n);
            }
            break;
        }   
    }

    if (n == -1)
    {
        printf("You must specify a value with -n\n");
        return 1;
    }

    printf("The %d Fibonacci term is %lu\n", atoi(optarg), array[n]);

    return 0;
}

The problem is here:

 printf("The %d Fibonacci term is %lu\n", atoi(optarg), array[atoi(optarg)]);

When getopt has no more arguments to process, optarg is set to NULL. The function atoi has no proper error checking leading to SIGSEGV.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM