Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hackerrank圆形阵列旋转分割错误_C_Arrays_Pointers - Fatal编程技术网

Hackerrank圆形阵列旋转分割错误

Hackerrank圆形阵列旋转分割错误,c,arrays,pointers,C,Arrays,Pointers,我下面的代码在示例输入中成功运行,但在13个测试用例中给出了分段错误 #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int n; int k;

我下面的代码在示例输入中成功运行,但在13个测试用例中给出了分段错误

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n; 
    int k; 
    int q; 
    int index[q];
    scanf("%d %d %d",&n,&k,&q);
    int *a = (malloc(sizeof(int) * n));
    for(int a_i = 0; a_i < n; a_i++){
       scanf("%d",&a[a_i]);
    }
    for(int a0 = 0; a0 < q; a0++){
        int m; 
        scanf("%d",&m);
        index[a0] = m;
    }
    for(int i=0; i<k; i++){
        int ap = a[n-2];       

        for(int p=1; p<n-1; p++){
            a[p] = a[p-1];
        }
        a[0] = a[n-1];
        a[n-1] = ap;      


    }
    for(int j=0; j<q;j++){
        printf("%d\n", a[index[j]]);
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括


使用malloc()声明a可能会导致分段错误,因为它不会检查分配错误,但即使我将a定义为数组,问题仍然存在。

q未初始化,但用作数组声明的参数


在读取q的值后,您应该使用malloc分配索引数组。

购买其中一个有故障的输入(应该是5个hackos),然后使用
gdb
.Hmm。。。有趣:
intq;整数指数[q]否@kartikeykant18您不能。。。而且这里q是未初始化的编译?你怎么能做出这样的打字错误?如果那真的是你的程序,你会复制/粘贴。但我的观点是:当
n<2
intap=a[n-2]会给你带来麻烦。