#include<stdio.h> typedef struct Test{ float Item1; char Item2; int Item3; short Item4; long Item5; double Item6; }TestStr1; int main(){ struct Test TestStr; void *Ptr; TestStr.Item1 =1.11; TestStr.Item2 =2; TestStr.Item3 =33; TestStr.Item4 =44; TestStr.Item5 =55; TestStr.Item6 =66.6; printf("offsets: Item1=%ld; Item2=%ld; Item3=%ld Item4=%ld Item5=%ld Item6=%ld\n", (long) &(((TestStr1*)0)->Item1), (long) &(((TestStr1*)0)->Item2), (long) &(((TestStr1*)0)->Item3), (long) &(((TestStr1*)0)->Item4), (long) &(((TestStr1*)0)->Item5), (long) &(((TestStr1*)0)->Item6)); Ptr = (char *)&TestStr; printf("Item1 = %f\r\n" ,*(float *)(Ptr +(long) &(((TestStr1*)0)->Item1))); printf("Item2 = %d\r\n" ,*(char *)(Ptr +(long) &(((TestStr1*)0)->Item2))); printf("Item3 = %d\r\n" ,*(int *)(Ptr +(long) &(((TestStr1*)0)->Item3))); printf("Item4 = %d\r\n" ,*(short *)(Ptr +(long) &(((TestStr1*)0)->Item4))); printf("Item5 = %d\r\n" ,*(long *)(Ptr +(long) &(((TestStr1*)0)->Item5))); printf("Item6 = %f\r\n" ,*(double *)(Ptr +(long) &(((TestStr1*)0)->Item6))); return 0; }
标签:变量,Item2,TestStr1,long,C语言,地址,TestStr,printf,Ptr From: https://www.cnblogs.com/bboy110/p/17792241.html