当前位置:网站首页>Custom implementation offsetof

Custom implementation offsetof

2022-06-11 21:54:00 Code loving students

What is? offsetof?

offsetof, Programming language , This macro is used to find the offset of a member in the structure .

The header file :stddef.h

Macro form :

 size_t offsetof( structName, memberName )

explain :
1. The first parameter is the name of the structure , The second parameter is the name of the structure member .

2. The macro returns the structure structName Member of the memberName The offset . The offset is size_t Type of .

The code implementation is as follows :

typedef struct S
{
	char a;
	char c;
	int b;
}S;
#define OFFSETOF(type,name) ((int)&(((type*)0)->name))
int main()
{
	printf("%d\n", OFFSETOF(S, a));
	printf("%d\n", OFFSETOF(S, c));
	printf("%d\n", OFFSETOF(S, b));
	return 0;
}

原网站

版权声明
本文为[Code loving students]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112135378910.html