offsetof
offsetof
Retrieves the offset of a member from the beginning of its parent structure.返回结构成员相对于结构开头的字节偏移量。
size_t offsetof( structName, memberName );
Routine | required Header | Compatibility |
offsetof | <stddef.h> | ANSI, Win 95, Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
offsetof returns the offset in bytes of the specified member from the beginning of its parent data structure. It is undefined for bit fields.该宏返回类型为 size_t 的值,表示 type 中成员的偏移量。
structName
Name of the parent data structure
memberName
Name of the member in the parent data structure for which to determine the offset
Remarks
The offsetof macro returns the offset in bytes of memberName from the beginning of the structure specified by structName. You can specify types with the struct keyword.
Note offsetof is not a function and cannot be described using a C prototype.
e.g:
#include "stdafx.h"
#include "stddef.h"
#include "Afx.h"
typedef struct STUDENT_INFO
{
CString csName;
unsigned int wID;
int iarrScore[3];
unsigned int wRanking;
}STUDENT;
int main()
{
printf("STUDENT 结构中的 csName 偏移 %d 字节。\n",offsetof(STUDENT,csName));
printf("STUDENT 结构中的 wID 偏移 %d 字节。\n",offsetof(STUDENT,wID));
printf("STUDENT 结构中的 iarrScore 偏移 %d 字节。\n",offsetof(STUDENT,iarrScore));
printf("STUDENT 结构中的 wRanking 偏移 %d 字节。\n",offsetof(STUDENT,wRanking));
return true;
}
相关阅读
一、简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete。程序员忘记 delete,流程太复杂,最终导致
今天看书看到侯捷的《STL源码剖析》里提到trivial和non-trivial及POD类型,查了些资料理解了一下。trivial意思是无意义,这个triv
(1)函数指针与指针函数 (1)函数指针 一种特殊的指针,它指向函数的入口; /* * 定义一个函数指针p,只能指向返回值为int,形参为两个int的函
// 打印函数 template <typename T> void disp(T i) { cout<<i<<endl; } int main() { int i = 1; int *p = &i;
我们想定义一个全局变量,能够在多个文件中使用,举例说明比如说三个文件main.c hello.c hello.h想在main.c和hello.c中使用一个名字