Why does this code fail with a non-planet command line argument?

65 views Asked by At

I was working on a textbook example of a C program that will take command arguments and determine whether or not they are planets. This is the code I have created:

#include <stdio.h>
#include <string.h>
#define NUM_PLANETS 9

void main(int argc, char *argv[]){
    char *planets[] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn"
                        "Uranus", "Neptune", "Pluto"};
    printf("Num args: %d\n", argc);
    
    int i, j;
    
    for(i = 1; i < argc; i++){
        //for every argument on the command line
        printf("arg: %d\n", i);
        for(j = 0; j < NUM_PLANETS; j++){
            //for every planet
            printf("j = %d\n", j); 
            if(strcmp(argv[i], planets[j]) == 0){
                //argument is a planet
                printf("%s is planet %d\n", argv[i], j + 1);
                break;
            }
        }
        printf("hello\n");
        //check planet status
        if(j == NUM_PLANETS){
            printf("%s is not a planet\n", argv[i]);
        }
        printf("hello\n");
    }
}

When given .\planet.exe Jupiter Ted the program returns:

Num args: 3
arg: 1
j = 0
j = 1
j = 2
j = 3
j = 4
Jupiter is planet 5
hello
hello
arg: 2
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
j = 6
j = 7
j = 8

Evidently, there seems to be an issue with the program reaching the if statement which checks if the argument did not match any of the planets, but only when an argument does not already meet this requirement.

1

There are 1 answers

3
MikeCAT On BEST ANSWER

The array

char *planets[] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn"
                    "Uranus", "Neptune", "Pluto"};

has only following 8 elements:

"Mercury"
"Venus"
"Earth"
"Mars"
"Jupiter"
"SaturnUranus"
"Neptune"
"Pluto"

because there are no commas between "Saturn" and "Uranus" and therefore they are concatenated and treated as one string literal.

For that reason, accessing planets[8] is invoking undefined behavior.

Add comma between them to fix.