gpt4 book ai didi

C 段错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:48:56 25 4
gpt4 key购买 nike

#include <stdio.h>

void lergab(char *gab);
void criamatriz (char **alunoresp, int *resp);

int main(){
char gab[20];
char alunoresp [100][21];
int resp[80];

//lergab(&gab);

criamatriz (&alunoresp[100][21], &resp[80]);

printf ("\n\n%d\n", alunoresp[0][0]);

printf ("%c\n", alunoresp[0][1]);
printf ("%c\n", alunoresp[0][1]);
printf ("%c\n", alunoresp[0][19]);






return 0;
}

void lergab (char *gab){
int i;
for(i=0; i<20; i++)
scanf("%c\n" , &gab[i]);
printf("%c", gab[19]);
}

void criamatriz (char **alunoresp,int *resp){
int i = 0, j;

//for (i = 0; i <= 100; i++){

scanf ("%c", &alunoresp[i][0]);

for (j = 0; j < 80; j++){
scanf ("%d", &resp[j]);
}

for (j = 0; j < 80; j=j+4){

int g = 1;

if ((resp[j] + resp[j+1] + resp[j+2] + resp[j+3]) != 1)
alunoresp[i][g] = 'e';
else if (resp[j] == 1)
alunoresp[i][g] = 'a';
else if (resp[j+1] == 1)
alunoresp[i][g] = 'b';
else if (resp[j+2] == 1)
alunoresp[i][g] = 'c';
else if (resp[j+3] == 1)
alunoresp[i][g] = 'd';

g++;

}
//}
}

这个代码应该读取一个数字作为ASCII码,然后读取80个数字并将它们转换为各自的字符,就像一个电子测试扫描仪。IE:001个0是c,1个000是一个,1个01个0是一个无效的选项(因为你应该只在每个问题的答案上签名)。

代码只是一个测试,我的目标是创建一个矩阵,其中包含 100 个候选人,每个候选人有 20 个答案,以及他们的识别码,这是第一个读作 ASCII 的数字,因此 alunoresp[100][21 ] 被宣布。每次我尝试在 gcc 上运行它时都会遇到段错误,有什么想法吗?

编辑:

感谢您在索引方面的帮助。我没有注意到。尽管如此,即使在修复之后我仍然遇到段错误。

代码现在看起来像这样:

#include <stdio.h>

void lergab(char *gab);
void criamatriz (char **alunoresp, int *resp);

int main(){
char gab[20];
char alunoresp [100][21];
int resp[80];

//lergab(&gab);

criamatriz (&alunoresp, &resp);

printf ("\n\n%d\n", alunoresp[0][0]);

printf ("%c\n", alunoresp[0][1]);
printf ("%c\n", alunoresp[0][1]);
printf ("%c\n", alunoresp[0][19]);






return 0;
}

void lergab (char *gab){
int i;
for(i=0; i<20; i++)
scanf("%c\n" , &gab[i]);
printf("%c", gab[19]);
}

void criamatriz (char **alunoresp,int *resp){
int i = 0, j;

//for (i = 0; i <= 100; i++){

scanf ("%c", &alunoresp[i][0]);

for (j = 0; j < 80; j++){
scanf ("%d", &resp[j]);
}

for (j = 0; j < 80; j=j+4){

int g = 1;

if ((resp[j] + resp[j+1] + resp[j+2] + resp[j+3]) != 1)
alunoresp[i][g] = 'e';
else if (resp[j] == 1)
alunoresp[i][g] = 'a';
else if (resp[j+1] == 1)
alunoresp[i][g] = 'b';
else if (resp[j+2] == 1)
alunoresp[i][g] = 'c';
else if (resp[j+3] == 1)
alunoresp[i][g] = 'd';

g++;

}
//}
}

最佳答案

你的程序有两个问题:

  • 您错误地调用了 criamatriz 函数 - 您应该传递数组本身而不是将指针传递给数组的末尾,并且
  • 您的 criamatriz 函数错误地采用了二维数组 - 您将其作为指向指针的指针传递,这是不正确的。您应该像传递指向 21 个元素的数组的指针一样传递它。

下面是您应该如何声明和定义您的 criamatriz 函数:

void criamatriz(char (*alunoresp)[21], int *resp);

这样调用它:

criamatriz(alunoresp, resp);

关于C 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26389624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com