关于编译错误--提领指向不完全类型的指针

      前段时间调试c语言程序时,对一结构体的成员变量进行访问时,编译无法通过,
编译器错误提示为“提领指向不完全类型的指针”。

      因程序调用了一些动态链接库,里面使用了不些在动态库中定义的结构,为了了解程序的运行过程,
想知道结构体中的成员变量的值,加上些调试语句,如printf什么的,竟然无法通过编译了。

     一开始有点无解,可以通过结构体自身定义的函数进行访问,为什么直接操作的结构体时就出现编译错误,
翻了翻以前的c语言书才知道,对于结构体来说虽然所有的变量默认都是公有的,但是,如果想访问里面的成员变量,
必须有包含对其结构的定义,否则出现如上的错误,c编译器将无法识别结构里面的变量,一般想操作结构体里的成员变量,
都是通过调用结构体中定义的函数接口,来取得结构体中的值,如果想直接访问,在头文件或源文件中加入
结构体的定义就行了。

这种情况出现的可能有(test.h test.c)生成一动态链接库(test.so),使用do.c文件进行测试,如下:
  1. //file name : test.h
  2. typedef struct _Test Test;
  3. void set_value(Test *test, int value);
  4. int  get_value(Test *test);
  5. void set(Test *test);
  1. //file name : test.c
  2. #include "test.h"
  3. struct _Test 
  4. {
  5.     int value;
  6. };
  7. void set_value(Test *test, int v)
  8. {
  9.     test->value = v;
  10. }
  11. int get_value(Test *test)
  12. {
  13.     return test->value;
  14. }
  15. void set(Test *test)
  16. {
  17.     test->value = 100;
  18. }
  1. //生成动态库test.so
  2. # gcc -fpic -shared -o test.so test.c test.h
  1. //file name : do.c
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include "test.h"
  5. int main()
  6. {
  7.     Test *test = (Test *)malloc(sizeof(Test *));
  8.     test->value = 20;
  9.     printf("%d/n", get_value(test));
  10.     set_value(test, 10);
  11.     printf("%d/n", get_value(test));
  12.     set(test);
  13.     printf("%d/n", get_value(test));
  14.     return 0;
  15. }
  1. #gcc do.c ./test.so -o do
  2. #错误: 提领指向不完全类型的指针  //因为在do.c文件中只包含了test.h对结构的声明,没有实际的定义
将第10行的“test->value = 20;"注释后,便可通过编译,而且程序可以通过test.h定义的方法来操作成员变量的值。
如果在do.c中加入对Test结构体的描述便可以直接访问成员变量。
  1. //file name : do.c
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include "test.h"
  5. struct _Test
  6. {
  7.     int value;
  8. };
  9. int main()
  10. {
  11.     Test *test = (Test *)malloc(sizeof(Test *));
  12.     test->value = 20;
  13.     printf("%d/n", get_value(test));
  14.     set_value(test, 10);
  15.     printf("%d/n", get_value(test));
  16.     set(test);
  17.     printf("%d/n", get_value(test));
  18.     return 0;
  19. }
  1. #gcc do.c ./test.so -o do   //通过编译,执行
  2. #./do                                 //显示正常调用的结果
  3. #20
  4. #10
  5. #100



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值