offsetof(如何通过offsetof 动态的给结构体或者类成员变量赋值)

本文目录
- 如何通过offsetof 动态的给结构体或者类成员变量赋值
- offsetof函数什么意思啊
- #define offsetof(s,m) (size_t)&(((s*)0)->m) 请高手详解上述宏定义的所作的工作
- c语言中container_of, typeof, offsetof这些是哪个标准的
- offset在C语言中使用是什么意思啊
如何通过offsetof 动态的给结构体或者类成员变量赋值
举例说明:定义int型指针变量int * p;和int型变量int a;那么将a的地址值赋值给p:p=&a;另一种方式是动态方式:p = (int*)malloc(size(int));
offsetof函数什么意思啊
offsetof宏的简介
定义
在stddef.h头文件中,该宏的完整说明如下:
#ifdef __cplusplus
#ifdef _WIN64
#define offsetof(s,m) (size_t)( (ptrdiff_t)&reinterpret_cast《const volatile char&》((((s *)0)-》m)) )
#else
#define offsetof(s,m) (size_t)&reinterpret_cast《const volatile char&》((((s *)0)-》m))
#endif
#else
#ifdef _WIN64
#define offsetof(s,m) (size_t)( (ptrdiff_t)&(((s *)0)-》m) )
#else
#define offsetof(s,m) (size_t)&(((s *)0)-》m)
#endif
#endif /* __cplusplus */
功能
该宏用于求结构体中一个成员在该结构体中的偏移量。
在msdn上,该宏被写作:
size_t offsetof( structName, memberName );
第一个参数是结构体的名字,第二个参数是结构体成员的名字。该宏返回结构体structName中成员memberName的偏移量。偏移量是size_t类型的。
本段
程序示例
#include 《stdio.h》
#include 《stddef.h》
typedef struct
{
int iVal;
int iVal2;
}Test;
typedef struct
{
char ch;
int iNum;
}Test2;
int main(void)
{
Test t = {1, 2};
Test2 t2 = {’t’, 100};
printf(“\naddress of t : %p\naddress of t.iVal : %p\naddress of t.iVal2: %p\n\n“, &t, &(t.iVal), &(t.iVal2));
printf(“offset of iVal in t: %p\n“, offsetof(Test, iVal));
printf(“offset of iVal2 in t: %p\n“, offsetof(Test, iVal2));
printf(“\naddress of t2 : %p\naddress of t2.ch : %p\naddress of t2.iNum: %p\n\n“, &t, &(t2.ch), &(t2.iNum));
printf(“offset of ch in t2: %p\n“, offsetof(Test2, ch));
printf(“offset of iNum in t2: %p\n“, offsetof(Test2, iNum));
return 0;
}
在VS2005中输出:
address of t : 0012FF10
address of t.iVal : 0012FF10
address of t.iVal2: 0012FF14
offset of iVal in t: 00000000
offset of iVal2 in t: 00000004
address of t2 : 0012FF10
address of t2.ch : 0012FF00
address of t2.iNum: 0012FF04
offset of ch in t2: 00000000
offset of iNum in t2: 00000004
需要注意的是,Test2中iNum成员的偏移量
#define offsetof(s,m) (size_t)&(((s*)0)->m) 请高手详解上述宏定义的所作的工作
这里定义的是,一个宏,用宏来实现函数的功能。
如果你在下面调用了
offsetof(2,4);
他就等于
(size_t)&(((2*)0)-》4) ;
宏定义的这样方式,编译阶段不对函数的参数进行检查,所以这样是一种很不好的用法,
c语言中container_of, typeof, offsetof这些是哪个标准的
1. container_of是Linux内核中实现的宏,不是C语言的标准函数。不能跨平台。
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)-》member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
2. typeof是GNU C的扩展,不是ISO标准中的函数。用gcc编译可以跨平台。
3. offsetof是C语言标准库中的宏,定义在头文件stddef.h中。可以跨平台。
offset在C语言中使用是什么意思啊
意思是偏移值,通常用于指明一个位置,它的用法是把一个基底位置加上 offset 值 (offset 可以是负数)。 例如我们说 : 一个资料结构在内存中的位置是 0x125000,它的 offset 0x10 处是用户名字, offset 0x14 是用户序号。在 PE 结构中, offset 通常用于指明在 exe 档案中的实际位置
在手机的SEEM修改里,Offset就是数值的坐标位置,如:offset64就是指左边的横坐标0x060(行),上边的纵坐标x04(列);offset1E,就是指左边的横坐标0x010(行),上边的纵坐标x0E(列)。
lrc歌词中会经常见到[offset:500]这样的说明,指的是卡拉OK歌词滚动时的偏移量
C语言中的宏定义:
offsetof (type,member)
返回值:2个地址的偏移量,第一个地址是结构体名字,第二个地址是结构体成员,
所以返回的是二者之间的以byte为单位的偏移量
由于c++中struct已经强化为类,the use of offsetof is restricted to “POD types“.
例子:
/* offsetof example */
#include 《stdio.h》
#include 《stddef.h》
struct mystruct
{
char singlechar;
char arraymember;
char anotherchar;
};
int main ()
{
printf (“offsetof(mystruct,singlechar) is %d\n“,offsetof(mystruct,singlechar));
printf (“offsetof(mystruct,arraymember) is %d\n“,offsetof(mystruct,arraymember));
printf (“offsetof(mystruct,anotherchar) is %d\n“,offsetof(mystruct,anotherchar));
return 0;
}
Offset:引用函数,可以引用区域和单元格
语法:=Offset(reference,rows,cols,height,width)第一个参数是原点,第二个参数是
偏移的行,第三个参数是偏移的列,第四个参数是行高,第五个参数是列宽

更多文章:
turtles歌曲(哪位大神有turtles(乌龟组合)的<谢谢>的歌词中文翻译 感激不尽)
2026年10月11日 10:00
金山铁路22号线(请问现在轨道交通22号线金山铁路是个什么情况据说9月28日就开通了啊~~~)
2026年10月11日 09:10
repercussions(都是余波,repercussions和aftermath有什么区别啊)
2026年10月11日 09:00
dropdownlist 绑定(DropDownList 绑定所有项 并 显示指定项)
2026年10月11日 08:50
易语言网页api接口怎么调用(易语言,怎么读取网页json的api)
2026年10月11日 08:00
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40



