gpt4 book ai didi

c - C 中的段错误在 main 中比较从结构到数组的整数成员

转载 作者:行者123 更新时间:2023-11-30 15:04:31 24 4
gpt4 key购买 nike

我在下面的行中遇到段错误。

if((rel[i].node1 == SI && rel[i].node2 == gates[j]) || (rel[i].node2 == SI && rel[i].node1 == gates[j])

现在,所有变量/成员都是整数并且它们都已初始化。我真的不知道该怎么办。我也尝试过更改索引。

希望能得到一些帮助,谢谢。

代码:

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

/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
struct relationship {
int node1;
int node2;
};

int main()
{

int N; // the total number of nodes in the level, including the gateways
int L; // the number of links
int E; // the number of exit gateways
scanf("%d%d%d", &N, &L, &E);

int gates[E];
struct relationship rel[L];

for (int i = 0; i < L; i++) {
int N1; // N1 and N2 defines a link between these nodes
int N2;
scanf("%d%d", &N1, &N2);
rel[i].node1 = N1;
rel[i].node2 = N2;
}

for (int i = 0; i < E; i++) {
int EI; // the index of a gateway node
scanf("%d", &EI);
gates[i] = EI;
}

while (1) {
int SI; // The index of the node on which the Skynet agent is positioned this turn
scanf("%d", &SI);

int i, j;
for(i=0; i < L; i++){

//for(j=0; j <E; j++){

if((rel[i].node1 == SI && rel[i].node2 == gates[j]) || (rel[i].node2 == SI && rel[i].node1 == gates[j])) {
printf("%d %d ood\n", rel[i].node1, rel[i].node2);
rel[i].node1 = -1;
rel[i].node2 = -1;

}
// }
/*if(SI == rel[i].node1 || SI ==rel[i].node2){
printf("%d %d %d \n", rel[i].node1, rel[i].node2, SI);
//printf("%d %d\n", rel[i].node1, rel[i].node2);
rel[i].node1 = -1;
rel[i].node2 = -1;
break;
}*/
}
}

return 0;
}

最佳答案

您没有初始化 j,因此 gates[j] 最终出现段错误。

关于c - C 中的段错误在 main 中比较从结构到数组的整数成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40263522/

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