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;
}
posted @ 2021-11-09 17:05  hostid  阅读(210)  评论(0编辑  收藏  举报