简体   繁体   English

C中的scanf数组限制?

[英]Scanf array limit in C?

I have some little error in C: 我在C中有一些小错误:

Error: expression must have a constant value 错误:表达式必须具有恒定值

I know, that's mean that my limit must have a constant value, but how i can to solve that when i have this situation? 我知道,这意味着我的限额必须具有恒定值,但是当我遇到这种情况时,我该如何解决呢?

printf("Type limit: ");
scanf("%i",&limit);
int arr[limit];

Thanks. 谢谢。

EDIT : 编辑

Ok guys, another problem, sorry if i spam. 好的,另一个问题,抱歉,如果我是垃圾邮件。

    int num,limit,i;
    printf("Type limit: ");
    scanf("%i",&limit);
    int *arr = (int*)malloc(limit*sizeof(int));
    for(i=0;i<limit;i++)
    {
        printf("Type num %i: ",i);
        arr[i] = scanf("%i",&num);
    }
    system("pause");
    return 0;

error 4 error c2109 subscript requires array or pointer type 错误4错误c2109下标需要数组或指针类型

You should use malloc : 您应该使用malloc

printf("Type limit: ");
scanf("%i",&limit);
int *arr = malloc(sizeof(int) * limit);

Variable-length arrays with automatic storage duration are allowed since C99. 自C99开始,允许使用具有自动存储持续时间的可变长度数组。 In C89, it is not possible to allocate an array with automatic storage duration, size of which is not known at the compile time. 在C89中,不可能分配具有自动存储持续时间的数组,该数组的大小在编译时未知。 Use malloc to allocate it dynamically: 使用malloc动态分配它:

printf("Type limit: ");
scanf("%i", &limit);

int* arr = malloc(limit * sizeof(int));

and don't forget to call free(arr) to deallocate this memory once you don't need it anymore. 并且一旦您不再需要调用free(arr)来释放该内存,别忘了。


To your question about initializing this array with values being read from stdin in a loop: 关于使用循环中从stdin读取的值初始化此数组的问题:

for(i = 0; i < limit; ++i)
    arr[i] = scanf("%i", &num);

reads each value, stores it into num variable and then 1 is assigned into arr[i] since scanf returns "number of input items successfully matched and assigned" (which is 1 in this case). 读取每个值,将其存储到num变量中,然后将1分配给arr[i]因为scanf返回“成功匹配并分配的输入项数” (在这种情况下为1)。 You can read into array elements directly: 您可以直接读入数组元素:

for(i = 0; i < limit; ++i)
    scanf("%i", &arr[i]);

C89 and earlier versions of C didin't support run-time sizing of arrays. C89和早期版本的C不支持数组的运行时大小调整。 You need to turn on C99 (or newer) support in your compiler. 您需要在编译器中打开C99(或更高版本)支持。

If you are using Linux you can either type: 如果您使用的是Linux,则可以输入:

gcc -std=c99

or 要么

c99

to compile code written for c99. 编译为c99编写的代码。

Setting std=c99 flag in GCC 在GCC中设置std = c99标志

int *arr=malloc( limit*sizeof(int) );

This will allocated sufficient memory in the heap for your array of limit int 's. 这将为您的limit int数组在堆中分配足够的内存。 But this array will be “dynamical” (the size is set at run-time), and it will be your responsibility to “ free ” this memory when you not longer need it. 但是此数组将是“动态的”(大小是在运行时设置的),当您不再需要此内存时,您有责任“ free ”该内存。 Your variable arr will be just a pointer to that memory. 您的变量arr将只是指向该内存的指针。 int arr1[10]; on the other hand separate a memory space for 10 int in the stack, and your variable arr1 is that memory. 另一方面,在堆栈中为10 int分开一个内存空间,而变量arr1是该内存。 The compiler need to know the size. 编译器需要知道大小。 If you past it to a function taking int* it will “decay" to int*, that is, a pointer to the first element arr1[0]. 如果将其粘贴到采用int*的函数,它将“衰减”为int*,即指向第一个元素arr1[0].的指针arr1[0].

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

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