- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在尝试使用宏连接 (##) 和 offsetof 宏来生成标识符,但没有成功:
CMain.c:55: warning: implicit declaration of function 'Color__builtin_offsetof'
struct Color_methods_TABLE
{
struct
{
int8 getColor;
}
methods;
};
#define METHOD_NAME1(clazz, method) clazz##method
#define METHOD_NAME(clazz, method) METHOD_NAME1(clazz, method)
#define tdef_name(clazz, method) METHOD_NAME(clazz, method)
int main()
{
tdef_name(Color, offsetof(struct Color_methods_TABLE, methods.getColor));
return 0;
}
输出应该是:
Color0
其中 0 是 offsetof 宏的结果。这可以做到吗?感谢您的宝贵时间。
最佳答案
你不能为所欲为。 offsetof
(与 sizeof
一样)是在预处理之后,在编译过程中计算的。
查看源代码的预处理形式。使用 GCC,您可以使用 gcc -C -E CMain.c > CMain.i
获取它,然后使用编辑器(或寻呼机)查看 CMain.i
内部,例如少 CMain.i
关于将 offsetof 宏的结果连接 (##) 到标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22118880/
使用 OFFSETOF 宏更新结构字段的代码 #include #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
刚看到What does the following macro do?我要问我自己的问题:为什么这么多应用程序的 header 定义 offsetof他们自己? 有什么原因吗?不靠谱? 最佳答案
来自 C++ 标准: A standard-layout class is a class that: — has no non-static data members of type non-sta
我想知道一个简单的宏 offset_of_ 是否需要对 not 的指针取消引用。例如,一个 C++(意味着这段代码将使用 C++ 编译器编译)结构,它声明了 packed 属性 struct A {
我想要 mystruct1 中的 offsetof() 参数行。我试过了 offsetof(struct mystruct1, rec.structPtr1.u_line.line) 还有 offse
就在这个问题因重复而被驳回之前,大多数(如果不是全部)问题都已接受答案,并带有过时/未定义的行为解决方案。 问题: 有没有办法在编译时获得指向成员数据的指针的偏移量: 不依赖于未定义的行为(nullp
我编写了一个小函数来获取结构内部字段的字节偏移量,但为了实现这一点,我每次都会迭代这些字段,直到找到符号。然而,在 C 中,offsetof() 仅在编译时计算一次,因为结构内部的偏移量不再改变。我想
当我这样定义结构时: struct abc{ int member1; int arr_member1; std::vector mVector; } 并且想要创建一个 arr_member 变
我必须在已经存在的大代码中添加一段代码。我的代码中有一个指针,*Ptr。该指针的类型为 unsigned char。 无符号字符 *Ptr; 我有以下结构。 struct { uint8_t t
相关文章: [c 循环双链表:rev 遍历为同一节点提供不同的列表指针地址](c circular double linked-list: rev traverse gives different l
这个问题已经有答案了: 已关闭11 年前。 社区去年审查了是否重新开放此问题,并将其关闭: 原始关闭原因未解决 Possible Duplicate: Why does this C code wor
根据我的另一个问题,这是一个我想避免 offsetof 的具体示例。 为了与 glVertexAttribPointer 一起使用,我必须对最后一个参数使用 offsetof。 glVertexAt
我想在这里实现“GetParent()”函数- class ChildClass; class ParentClass { public: .... ChildClass childO
我看到了offsetof这两个宏: #if defined(_MSC_VER) && !defined(_CRT_USE_BUILTIN_OFFSETOF) #ifdef __cplusplus
我有以下代码,以便能够访问结构数组中的多个字段(为简单起见,我已将其减少为两个)。最终指针计算的正确咒语是什么 *(ptr + offset) = 数据;因为我总是得到: 错误:从类型“int32_t
我一直在尝试使用宏连接 (##) 和 offsetof 宏来生成标识符,但没有成功: CMain.c:55: warning: implicit declaration of function 'Co
我正在尝试以下列方式使用 offsetof 宏: typedef unsigned char u8; typedef unsigned short u16; struct MapBlock { u
我正在尝试在我自己的另一个宏中使用 offsetof 宏,如下所示: #define MY_MACRO(struct_type, member) \ my_function(param1, o
这个问题在这里已经有了答案: Why does this implementation of offsetof() work? (8 个答案) 关闭 3 年前。 offsetof 在stddef.h
我试图通过使用 offsetof() 宏来学习如何访问结构成员,我遇到了这个例子 How can I access structure fields by name at run time?这一行:
我是一名优秀的程序员,十分优秀!