【发布时间】:2019-03-19 06:18:44
【问题描述】:
#include <stdio.h>
struct m_tag {
short m_tag_id;
short m_tag_len;
int m_tag_cookie;
};
struct packet_tags {
struct m_tag *slh_first;
}tags;
#define SFIRST(head) ((head).slh_first)
int main(void) {
printf("%p\n", SFIRST(&tags));
return 0;
}
在函数'main'中: 错误:在不是结构或联合的东西中请求成员“slh_first”
这段代码有什么问题?
【问题讨论】:
-
因为如果你看一个写着“湖这边”的标志,这个标志不是湖吗?
-
您是否尝试过不使用宏访问
slh_first?看起来怎么样? -
您的选择是:
stuctPointer->structMember或structObject.structMember但不是stuctPointer.structMember -
顺便说一句,你写的是 C 还是 C++?毕竟,你一次只能用一个编译器编译这个小程序。请不要为了吸引浏览而添加标签,这是对系统的滥用,并且会受到 C 和 C++ 社区的强烈反对。
-
此类宏在 C 中可能很有用,但在 C++ 中应避免使用,而是更喜欢内联(模板化)函数。