Scanf数组限制在C?

use*_*358 0 c arrays scanf variable-length-array

我在C中有一些小错误:

错误:表达式必须具有常量值

我知道,这意味着我的限制必须有一个恒定的价值,但是当我遇到这种情况时,我怎么能解决这个问题呢?

printf("Type limit: ");
scanf("%i",&limit);
int arr[limit];
Run Code Online (Sandbox Code Playgroud)

谢谢.

编辑:

好的,另一个问题,对不起,如果我垃圾邮件.

    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;
Run Code Online (Sandbox Code Playgroud)

错误4错误c2109下标需要数组或指针类型

Vin*_*ira 5

你应该使用malloc:

printf("Type limit: ");
scanf("%i",&limit);
int *arr = malloc(sizeof(int) * limit);
Run Code Online (Sandbox Code Playgroud)