error: invalid type argument of '->' (have 'int')|

error: invalid type argument of '->' (have 'int')|

我在创建使用邻接 LIST 表示创建图形的函数时遇到以下错误:

错误:“->”的无效类型参数(有 'int')

我已将出现此错误的行(在注释中)标记为 //error。下面是代码:

typedef struct GRAPH
{
    int V;
    int E;
    int *adj; //head pointer to the Linked List
} graph;

typedef struct NODE //Node of the Linked List
{
    int vertexNumber;
    struct NODE *next;
} node;

graph *adjListOfGraph()
{
    int i,x,y;
    node *temp;
    graph *g;
    g = (node *)malloc(sizeof(graph));
    if(!g)
    {
        printf("Memory Error in creating the graph");
        return;
    }
    scanf("Number of Vertex: %d, Number of Edges: %d", &g->V,&g->E);
    g->adj=(node *)malloc(g->V *sizeof(node));
    for(i=0;i<g->V;i++)
    {
        g->adj[i] = (node *)malloc(sizeof(node));
        g->adj[i]->vertexNumber = i;    //error
        g->adj[i]->next = g->adj[i];    //error
    }
    for(i=0;i<g->E;i++)
    { 
        scanf("Reading edges: %d %d", &g->V,&g->E);
        temp = (node *)malloc(sizeof(node));
        temp->vertexNumber = y;
        temp->next = g->adj[x];
        g->adj[x]->next = temp;   //error
        temp = (node *)malloc(sizeof(node));
        temp->vertexNumber =y;
        temp->next = g->adj[y];
        g->adj[y]->next = temp;   //error
    }
    return g;
}

请查看注释为错误的行。我搜索了很多,也尝试用 替换 -> 。但是没用。

编译器会报错,因为您试图取消引用一个 adj[j],它是一个 int。查看您的代码,似乎您可能想要

node **adj;

而不是

int *adj;

其他问题:

  • C中有is no need to cast the result of malloc (and family)
  • return; 应该是 return NULL; 因为函数 adjListOfGraph 被设计成 return a graph* 而不是什么都没有。
  • 请注意,scanf喜欢

    scanf("Number of Vertex: %d, Number of Edges: %d", &g->V,&g->E);
    

    要接受输入,您必须输入 "Number of Vertex: <some number>, Number of Edges: <some number>" 而不仅仅是 "<some number> <some number>"