简体   繁体   English

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

[英]Using scanf to get user inputs for dynamic arrays in c

When I try to get user input for the dynamic array, it stops at the first for loop.当我尝试获取动态数组的用户输入时,它在第一个 for 循环处停止。 It gives me an error that my program has stopped working.它给了我一个错误,我的程序已经停止工作。 Help me out!帮帮我!

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

Use this instead:改用这个:

scanf("%d", &number[index]);

Note the & operator before number[index] is needed when using scanf for integer input.请注意,当使用scanf进行整数输入时,需要在number[index]之前使用&运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM