C算法,已知结构体成员地址,如何得出结构体的基地址?
#include<stdio.h>
#define BASE_CR(Record, TYPE, Field) ((TYPE *) ((char *) (Record) - (char *) &(((TYPE *) 0)->Field)))
typedef struct _MyStruct
{
int a;
char b;
long c;
int d;
} MyStruct;
void main()
{
MyStruct myStruct = {10,'a',30,25};
printf("MyStruct Address 0x%x \n", &myStruct);
MyStruct * pMyStruct = BASE_CR(&(myStruct.c), MyStruct, c);
printf("pMyStruct Address 0x%x \n", pMyStruct);
return 0;
}
-------------------------------------- 适合自己的才是最好的!-----------------------------------------