gpt4 book ai didi

c - 段错误(核心已转储)C

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:11 24 4
gpt4 key购买 nike

我在使用指针时遇到了一些问题。我的这部分代码出现段错误:

int **adjacent_matrix[N][N];

void create_graph(int N, int L, int **adjacent_matrix, int *queue, int *integers, int *topological_sort){
int count, maximum_edges, origin_vertex, destination_vertex;
total_vertices = N;
maximum_edges = L;
int i =0;

for(count = 1; count <= maximum_edges; count++){
origin_vertex = integers[i]-1;
destination_vertex = integers[i+1]-1;
if(origin_vertex > total_vertices || destination_vertex > total_vertices || origin_vertex < 0 || destination_vertex < 0){
printf("Edge Co-ordinates are Invalid\n");
count--;
}
else{
//ERROR HERE !
adjacent_matrix[origin_vertex][destination_vertex] = 1;
}

if(count == maximum_edges){
break;
}
i+=2;
}
}

关于如何解决这个问题的任何想法?谢谢

编辑:主要功能在这里:(我在第一个 for 循环之后调用该函数)

int main(){

int i, N, L;

if(scanf("%d", &N)){};
if(scanf("%d", &L)){};

int **adjacent_matrix[N][N];
int queue[N];
int integers[N];
int topological_sort[N];

for(i=0; i<(L*2); i++){
if(scanf("%d", &integers[i])){};
}

int vertex, count, indegree[N];
create_graph(N, L, **adjacent_matrix, queue, integers, topological_sort);

for(i = 0; i < total_vertices; i++){
indegree[i] = find_indegree_of_vertex(i, **adjacent_matrix, queue, integers, topological_sort);
if(indegree[i] == 0){
add(N, i, **adjacent_matrix, queue, integers, topological_sort);
}
}
count = 0;
while(!isEmpty(**adjacent_matrix, queue, integers, topological_sort) && count < total_vertices){
vertex = del(**adjacent_matrix, queue, integers, topological_sort);
topological_sort[++count] = vertex;
for(i = 0; i < total_vertices; i++){
if(**adjacent_matrix[vertex][i] == 1){
/*adjacent_matrix[vertex][i] = 0;*/
indegree[i] = indegree[i] - 1;
if(indegree[i] == 0){
add(N, i, **adjacent_matrix, queue, integers, topological_sort);
}
}
}
}
if(count < total_vertices){
printf("Incoerente\n");
return EXIT_SUCCESS;
}
if(L < (N-1) || check_topological(N, **adjacent_matrix, queue, integers, topological_sort)==0){
printf("Insuficiente\n");
return EXIT_SUCCESS;
}

for(i = 1; i < count; i++){
printf("%d", topological_sort[i]+1);
printf(" ");
}
printf("%d\n", topological_sort[count]+1);

return EXIT_SUCCESS;
}

最佳答案

adjacent_matrix 的类型错误:您应该将其设为指向 int 数组的指针数组,或者将其定义为二维数组并更改另一个数组的原型(prototype)获取可变长度二维数组的函数。

第一种方法更简单且更可移植(对于 c99 之前的编译器):

int main(void) {
int i, N, L;

if (scanf("%d", &N) != 1) { exit(1); }
if (scanf("%d", &L) != 1) { exit(1); }

int *adjacent_matrix[N];
int queue[N];
int integers[N];
int topological_sort[N];

for (i = 0; i < N; i++) {
adjacent_matrix[i] = calloc(sizeof(*adjacent_matrix[i]), N);
}

for (i = 0; i < (L * 2); i++) {
if (scanf("%d", &integers[i]) != 1) { exit(2); }
}

int vertex, count, indegree[N];
create_graph(N, L, adjacent_matrix, queue, integers, topological_sort);

for (i = 0; i < total_vertices; i++) {
indegree[i] = find_indegree_of_vertex(i, adjacent_matrix, queue,
integers, topological_sort);
if (indegree[i] == 0) {
add(N, i, adjacent_matrix, queue, integers, topological_sort);
}
}
count = 0;
while (!isEmpty(adjacent_matrix, queue, integers, topological_sort)
&& count < total_vertices) {
vertex = del(adjacent_matrix, queue, integers, topological_sort);
topological_sort[++count] = vertex;
for (i = 0; i < total_vertices; i++) {
if (adjacent_matrix[vertex][i] == 1) {
adjacent_matrix[vertex][i] = 0;
indegree[i] = indegree[i] - 1;
if (indegree[i] == 0) {
add(N, i, adjacent_matrix, queue, integers, topological_sort);
}
}
}
}
if (count < total_vertices) {
printf("Incoerente\n");
return EXIT_SUCCESS;
}
if (L < (N - 1) || check_topological(N, adjacent_matrix, queue, integers, topological_sort) == 0) {
printf("Insuficiente\n");
return EXIT_SUCCESS;
}

for (i = 1; i < count; i++) {
printf("%d", topological_sort[i] + 1);
printf(" ");
}
printf("%d\n", topological_sort[count] + 1);

return EXIT_SUCCESS;
}

注意:代码中可能还有其他问题。

关于c - 段错误(核心已转储)C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43005308/

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