获取结构体某成员的偏移
#include <iostream> using namespace std; #define OFFSSEST(Type,member) size_t(&(((Type*)0)->member)) struct Student { int num; char sex; int age; float score; }; int main() { Student sdt1; Student *ptr=&sdt1; cout<<(int)&(ptr->age)<<endl; cout<<(int)&(ptr->age)-(int)ptr<<endl; size_t offest=OFFSSEST(Student,age); cout<<offest<<endl; return 0; }