计算结构体成员距起始地址的偏移量

#include
#include

struct S
{
	char c1;
	int a;
	char c2;
};

int main()//求解结构体成员距起始地址的偏移量
{
	struct S s ;
	printf("%d\n", offsetof(struct S, c1));
	printf("%d\n", offsetof(struct S, a));
	printf("%d\n", offsetof(struct S, c2));
	return 0;
}

//#define OFFSETOF(struct_name,mem_name) &(((struct_name*)0)->mem_name)
//
//struct S
//{
//	char c1;
//	int a;
//	char c2;
//};
//int main()
//{
//	printf("%d\n", OFFSETOF(struct S, c1));
//	printf("%d\n", OFFSETOF(struct S, a));
//	printf("%d\n", OFFSETOF(struct S, c2));
//	return 0;
//}

你可能感兴趣的:(算法,数据结构)