C-取消引用不完整类型的指针

use*_*321 2 c compiler-errors

我已经阅读了有关同一错误的5个不同问题,但是我仍然找不到我的代码有什么问题。

main.c

int main(int argc, char** argv) {
    //graph_t * g = graph_create(128); //I commented this line out to make sure graph_create was not causing this.
    graph_t * g;
    g->cap; //This line gives that error.
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

。C

struct graph {
    int cap;
    int size;
};
Run Code Online (Sandbox Code Playgroud)

。H

typedef struct graph graph_t;
Run Code Online (Sandbox Code Playgroud)

谢谢!

Mar*_*lon 5

您不能这样做,因为该结构是在另一个源文件中定义的。typedef的全部目的是向您隐藏数据。可能有诸如graph_capgraph_size可以调用的函数,这些函数将为您返回数据。

如果这是您的代码,则应struct graph在头文件中定义,以便所有包含此头的文件都可以具有其定义。