c/c++语言开发共享C – 取消引用指向不完整类型的指针

我已经阅读了关于同一错误的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; } 

。C

 struct graph { int cap; int size; }; 

。H

 typedef struct graph graph_t; 

谢谢!

    您不能这样做,因为结构是在不同的源文件中定义的。 typedef的重点是隐藏您的数据。 可能会调用graph_capgraph_size等函数来为您返回数据。

    如果这是您的代码,您应该在头文件中定义struct graph ,以便包含此头文件的所有文件都能够定义它。

    当编译器正在编译main.c它需要能够看到 struct graph的定义,以便它知道存在一个名为cap的成员。 您需要将结构的定义从.c文件移动到.h文件。

    如果您需要graph_t为不透明数据类型 ,则另一种方法是创建访问器函数,该函数采用graph_t指针并返回字段值。 例如,

    graph.h

     int get_cap( graph_t *g ); 

    graph.c

     int get_cap( graph_t *g ) { return g->cap; } 

    必须是您定义事物的顺序。 typedef行需要显示在包含main()的文件所包含的头文件中。

    否则它对我来说很好。

    lala.c

     #include "lala.h" 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; } 

    lala.h

     #ifndef LALA_H #define LALA_H struct graph { int cap; int size; }; typedef struct graph graph_t; #endif 

    这编译没有问题:

     gcc -Wall lala.c -o lala 

      以上就是c/c++开发分享C – 取消引用指向不完整类型的指针相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/549895.html

      (0)
      上一篇 2021年1月14日
      下一篇 2021年1月14日

      精彩推荐