用define封装结构体简化代码

#include <stdio.h>
#include <stdlib.h>

struct A{
        int a;
        char *p; 
};

struct B{
        int b;
        char *p; 
};

#define TYPE(type) \
struct {                \   
        struct type *content;   \   
}

#define MY(type) \
        struct type 

int main(){
        TYPE(A) x;
        x.content = (struct A *)malloc(sizeof(struct A));
        TYPE(B) y;
        y.content = (struct B *)malloc(sizeof(struct B));
    
        x.content->a = 1;
        y.content->b = 2;

        MY(A) a;
        MY(B) b;
    
        a.a = 10; 
        b.b = 11; 

        printf("%d %d %d %d\n", x.content->a, y.content->b, a.a, b.b);

        return 0;
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页