C语言_typedef、union

@(C语言)

[toc]

typedef

作用

设置别名,并没有创建新的类型

使用

定义一个二叉树:

struct Tnode{
    char * word;
    int count;
    
    struct Tnode * left;
    struct Tnode * right;
    
}

现在可以写成这样:

typedef struct Tnode* Treeptr;
typedef struct Tnode{
    char * word;
    int count;
    
    Treeptr left;
    Treeptr right;
    
} BinaryTreeNode;

int main(){

    BinaryTreeNode * node;
    node =(BinaryTreeNode *)malloc(sizeof(BinaryTreeNode *));
    return 0;
}

这样就很有面向对象的感觉

union

将不同的数据类型放到同一个内存中。
内存大小以最大的为准

union MyUnion{
    int a;
    char b;
    float c;
};

int main(){
    union MyUnion unio;
    unio.a =10;
    unio.b ='a';
    unio.c =22.2f;
    return 0;
}

enum

//自动升值
enum{
    monday=10 ,
    wenday,
};


int main(){

    printf("wenday : %d\n",wenday);
    return 0;
}

输出:
wenday : 11

你可能感兴趣的:(C语言_typedef、union)