c语言无效参数视为严重错误,C语言编译错误:错误:‘-’参数类型无效(有‘int’)...
一、編譯錯誤代碼:
#include int main()
{
#define offsetof(type, member) ((size_t) &((type *)0->member))
struct test
{
int a;
int b;
};
printf("offset of a %d\n", offsetof(struct test, a));
printf("offset of b %d\n", offsetof(struct test, b));
return 0;
}
二、錯誤信息:
test.c:5:57: 錯誤:‘->’參數類型無效(有‘int’)
#define offsetof(type, member) ((size_t) &((type *)0->member))
^
test.c:13:32: 附注:in expansion of macro ‘offsetof’
printf("offset of a %d\n", offsetof(struct test, a));
^~~~~~~~
test.c:5:57: 錯誤:‘->’參數類型無效(有‘int’)
#define offsetof(type, member) ((size_t) &((type *)0->member))
^
test.c:14:32: 附注:in expansion of macro ‘offsetof’
printf("offset of b %d\n", offsetof(struct test, b));
^~~~~~~~
三、錯誤原因
運算符"->"的優先級高于強轉的優先級,編譯器先取0->member的值
四、修改后代碼
#include int main()
{
#define offsetof(type, member) ((size_t) &(((type *)0)->member))
struct test
{
int a;
int b;
};
printf("offset of a %d\n", offsetof(struct test, a));
printf("offset of b %d\n", offsetof(struct test, b));
return 0;
}
總結
以上是生活随笔為你收集整理的c语言无效参数视为严重错误,C语言编译错误:错误:‘-’参数类型无效(有‘int’)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言周传生教材答案,C语言程序设计与实
- 下一篇: c语言rand随机输出字母,菜鸟求助,写