1 代码偏移指令offsetof
#include <stddef.h> #include <stdio.h>
#include <stddef.h> #include <stdio.h> typedef struct{ char name; int age1; int age2; int age3; int age4; int age5; }person; struct address { char aa; int bbb; person namePtr; char street[50]; int phone; }; int main() { printf("address 结构中的 bbb 偏移 = %ld 字节 ",offsetof(struct address, bbb)); printf("address 结构中的 age2 偏移 = %ld 字节 ",offsetof(struct address, namePtr)+offsetof(person, age2)); printf("address 结构中的 street 偏移 = %ld 字节 ",offsetof(struct address, street)); printf("address 结构中的 phone 偏移 = %ld 字节 ",offsetof(struct address, phone)); return(0); }
struct address { int aa; int bbb; int name[50]; char street[50]; int phone; }; int main() { printf("address 结构中的 name 偏移 = %ld 字节 ",offsetof(struct address, name)); printf("address 结构中的 street 偏移 = %ld 字节 ",offsetof(struct address, street)); printf("address 结构中的 phone 偏移 = %ld 字节 ",offsetof(struct address, bbb)); return(0); }