使用scanf获取c中动态数组的用户输入

问题描述 投票:0回答:1

当我尝试获取动态数组的用户输入时,它会在第一个for循环处停止。它给我一个错误,表明我的程序已停止工作。帮帮我!

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

int main(){
    int count = 0, sum = 0, *number = NULL, index = 0, size = 0;
    scanf("%d", &size);

    number = (int *)calloc(size, sizeof(int)); //Dynamic allocation

    printf("Enter the elements\n");
    for(index = 0; index < size; index++){
        scanf("%d",number[index]); //Getting the input values from user but i get an error
    }
    return 0;
}
c dynamic-arrays
1个回答
0
投票

改用它:

© www.soinside.com 2019 - 2024. All rights reserved.