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]);
}