美文网首页
C语言_typedef、union

C语言_typedef、union

作者: 拙峰朽木 | 来源:发表于2018-04-28 09:49 被阅读96次

    @(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

          本文链接:https://www.haomeiwen.com/subject/tcgwlftx.html