我最近开始研究这本关于算法和数据结构的书,从这本书中,我不仅在互联网上,而且在大学里也从我的算法设计老师那里听到了很多赞扬,最后我在书的153页(书本身)或165页(pdf格式)中使用了一些代码,出现了一些错误。
代码如下:

#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#define MAXV 1000
typedef struct  {
    int y;
    int weight;
    struct edgenode *next;
}edgenode;
typedef struct {
    edgenode *edges[MAXV + 1];
    int degree[MAXV + 1];
    int nvertices;
    int nedges;
    bool directed;
}graph;

void initialize_graph(graph *g, bool directed);
void read_graph(graph *g, bool directed);
void insert_edge(graph *g, int x, int y, bool directed);
void print_graph(graph *g);
void initialize_graph(graph *g, bool directed) {
    int i;

    g->nvertices = 0;
    g->nedges = 0;
    g->directed = directed;
    for (i = 1; i <= MAXV; i++) {
        g->degree[i] = 0;
        g->edges[i] = NULL;
    }

}

void read_graph(graph *g, bool directed) {
    int i;
    int m;
    int x, y;
    initialize_graph(g, directed);
    scanf("%d %d", &(g->nvertices), &m);
    for (i = 1; i <= m; i++) {
        scanf("%d %d", &x, &y);
        insert_edge(g, x, y, directed);
    }
}

void insert_edge(graph *g, int x, int y, bool directed) {
    edgenode *p;
    p = malloc(sizeof(edgenode));
    p->weight = NULL;
    p->y = y;
    p->next = g->edges[x];
    g->edges[x] = p;
    g->degree[x]++;
    if (directed == false)
        insert_edge(g, y, x, true);
    else
        g->nedges++;
}

void print_graph(graph *g) {
    int i;
    edgenode *p;
    for (i = 1; i <= g->nvertices; i++) {
        printf("%d ", i);
        p = g->edges[i];
        while (p != NULL) {
            printf(" %d", p->y);
            p = p->next;
        }
        printf("\n");
    }
}
main() {
    bool directed = true;
    graph *g;
    read_graph(g, directed);
    print_graph(g);
    system("pause");
}


以下是错误:
1>c:\users\dragos\source\repos\learninggraph\learninggraph\main.c(47):警告C4047:“=”:“int”与“void*”的间接级别不同
1>c:\users\dragos\source\repos\learninggraph\learninggraph\main.c(49):警告C4133:“=”:不兼容类型-从“edgenode*”到“edgenode*”
1>c:\users\dragos\source\repos\learninggraph\learninggraph\main.c(65):警告C4133:“=”:不兼容类型-从“edgenode*”到“edgenode*”
1>c:\users\dragos\source\repos\learninggraph\learninggraph\main.c(73):错误C4700:使用了未初始化的局部变量“g”
1>已完成生成项目“LearningGraph.vcxproj”--失败。
====生成:0成功,1失败,0最新,0跳过==========
我认为主要的问题是“不兼容的类型”,但我也很可能是错的。

最佳答案

insert_edge

 p->weight = NULL;

是无效的,因为weight是int,而指针是空的(通常(void*)0
insert_edge
 p->next = g->edges[x];

是无效的,因为next是未定义的类型struct edgenode *,但edges[x]edgenode *。为了解决这个问题你必须更换
typedef struct {
    int y;
    int weight;
    struct edgenode *next;
}edgenode;

通过
typedef struct edgenode {
    int y;
    int weight;
    struct edgenode *next;
}edgenode;

原因在print_graph行中相同
 p = p->next;

显式地将main的返回类型设置为int
在main中,用g never set/initialized调用read_graph,因此当在read_graph中取消引用时,行为是未定义的,这也是print_graph中的情况。只是替换
 graph *g;
 read_graph(g, directed);
 print_graph(g);

通过
graph g;
read_graph(&g, directed);
print_graph(&g);

完全修改版本:
#include <stdlib.h>
#include<stdio.h>
#include<stdbool.h>

#define MAXV 1000

typedef struct edgenode {
    int y;
    int weight;
    struct edgenode *next;
}edgenode;

typedef struct {
    edgenode *edges[MAXV + 1];
    int degree[MAXV + 1];
    int nvertices;
    int nedges;
    bool directed;
}graph;

void initialize_graph(graph *g, bool directed);
void read_graph(graph *g, bool directed);
void insert_edge(graph *g, int x, int y, bool directed);
void print_graph(graph *g);
void initialize_graph(graph *g, bool directed) {
    int i;

    g->nvertices = 0;
    g->nedges = 0;
    g->directed = directed;
    for (i = 1; i <= MAXV; i++) {
        g->degree[i] = 0;
        g->edges[i] = NULL;
    }

}

void read_graph(graph *g, bool directed) {
    int i;
    int m;
    int x, y;
    initialize_graph(g, directed);
    scanf("%d %d", &(g->nvertices), &m);
    for (i = 1; i <= m; i++) {
        scanf("%d %d", &x, &y);
        insert_edge(g, x, y, directed);
    }
}

void insert_edge(graph *g, int x, int y, bool directed) {
    edgenode *p;
    p = malloc(sizeof(edgenode));
    p->weight = 0;
    p->y = y;
    p->next = g->edges[x];
    g->edges[x] = p;
    g->degree[x]++;
    if (directed == false)
        insert_edge(g, y, x, true);
    else
        g->nedges++;
}

void print_graph(graph *g) {
    int i;
    edgenode *p;
    for (i = 1; i <= g->nvertices; i++) {
        printf("%d ", i);
        p = g->edges[i];
        while (p != NULL) {
            printf(" %d", p->y);
            p = p->next;
        }
        printf("\n");
    }
}

int main() {
    bool directed = true;
    graph g;
    read_graph(&g, directed);
    print_graph(&g);
    system("pause");
}

编制:
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra g.c
pi@raspberrypi:/tmp $

关于c - 测试和学习C语言图中的意外错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55874950/

10-10 07:16