由信号 SIGSEGV(地址边界错误)终止

terminated by signal SIGSEGV (Address boundary error)

我试图先向用户查询一些输入,然后用这些输入填充一个动态列表。然后以相反的顺序打印它们。出于某种原因,我收到 SIGSEGV 错误,但我找不到任何无限循环或地址错误。帮我找出问题所在。

#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;
}

尝试在启用所有警告的情况下编译您的代码(-Wallgcc)。您的线路:

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

应该是:

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