1

I am trying to take the arguments put on the command line and put them into an array of integers so I can do computations with them. What I am doing to solve this is taking arguments starting at argv[1] (I dont need ./a.out) and putting them into a character array where I then check to see if it is a digit. If so, I then put them into an integer array. My problem right now is that I am getting a bus error. My code is below. Any advice given is greatly appreciated. Thanks.

This is my input:

./a.out 2 3

and my output:

Good
Input 1 is good
Bus error: 10

Code:*

int main (int argc, char *argv[ ] )
{
    .... //declaration of variables

    if (argc == 3) {
        printf("Good\n");
    }
    else {
        printf("Wrong amount of inputs\n");
        exit(0);
    }

    for (i = 1; i < argc; i++) {
        //length = strlen(argv[i]);

        //theinputs[i] = malloc((length + 1) * (sizeof(char)));
        strcpy(theinputs[i - 1], argv[i]);
        if ( isdigit(*theinputs[i-1]) ) {
            printf("Input %d is good\n", i);
        }
        else {
            printf("One or more of your inputs is not an integer %s.\n", theinputs[i]);
            //return -1; //Inputs are not being read correctly
            exit(0);
        }
        numbers[i] = atoi(theinputs[i]);
    }

    for (i = 0; i < 2; i++) {
        printf("%d\n", numbers[i]);
    }
admdrew
  • 3,790
  • 4
  • 27
  • 39
KS7X
  • 332
  • 2
  • 15
  • 1
    You didn't allocate theinputs – Random Feb 19 '14 at 21:50
  • Thank you very much. Originally, I allocated it but got an error at the time, so I had set it as a comment temporarily. The bus error is gone. Thanks! – KS7X Feb 19 '14 at 22:18

0 Answers0