C 为什么它显示运行时错误?它没有给出任何输出?

C 为什么它显示运行时错误?它没有给出任何输出?,c,C,/此代码用于旋转n个条目k个次数的数组 在indexz q no of times处输出数组元素/my这里的问题是,它显示了运行时错误,为什么会发生这种情况。这个问题实际上来自黑客等级,在算法的实现部分,它的名称是循环数组旋转。这段代码有什么错误吗 #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int n

/此代码用于旋转n个条目k个次数的数组
在indexz q no of times处输出数组元素/my这里的问题是,它显示了运行时错误,为什么会发生这种情况。这个问题实际上来自黑客等级,在算法的实现部分,它的名称是循环数组旋转。这段代码有什么错误吗

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

int main() {

    int n,k,q;
    int a[n];
    scanf("%d%d%d",&n,&k,&q);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int j=0;j<k;j++)/*this is for rotating the array*/
    {
        int y=a[n-1];
        for(int x=n-2;x>=0;x--)
            a[x+1]=a[x];
        a[0]=y;
    }  
    for(int b=0;b<q;b++)
    {
        int z;
        scanf("%d",&z);
        printf("%d\n",a[z]);
    }
    return 0;
}
#包括
#包括
#包括
#包括
int main(){
int n,k,q;
int a[n];
scanf(“%d%d%d”,&n,&k,&q);
对于(int i=0;i问题:

int n,k,q;
int a[n];
在设置
n
的值之前,您正在创建一个大小为
n
的数组

使用:


“此代码中是否有任何错误”-是。
int a[n]
-你能告诉我这个数组的大小吗?请格式化你的代码以确保可读性。使用变量作为数组的大小是否合法?@hymie,是的。可变长度数组从C99开始就是标准的一部分。谢谢。我在那之前学过C很多年,没有跟上不断变化的标准。
int n,k,q;

// Read a value into n first
if ( scanf("%d%d%d",&n,&k,&q) != 3 )
{
   // Deal with error
   return 1;
}

// Then define the array.
int a[n];