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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容