Gargl Gargl - 8 days ago 5
C Question

terminated by signal SIGSEGV (Address boundary error)

I am trying to query the user for first a number of inputs, then fill a dynamic list with that number of inputs. Then print them in reverse order. For some reason I get the SIGSEGV error, but I cannot find any infinite loops or address error. Help me find what is wrong.

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

int main() {
int input;
printf("Enter a non-negative number: ");
scanf("%d", &input);

int *listA;
listA = (int*)malloc(sizeof(int[input]));
printf("Now, enter %d non-negative numbers: ", input);
for (int i = 0; i < input; i++) {
scanf("%d", listA[i]);
}
printf("In reverse order, your input is: ");
for (int i = input-1; i >=0; i-- ) {
printf("%d",listA[i]);
}
free(listA);
return 0;
}

Answer

Try compiling your code with all warnings enabled (-Wall with gcc). Your line:

scanf("%d", listA[i]);

should be:

scanf("%d", &listA[i]);