typedef

作者: Vergil_wj | 来源:发表于2021-07-25 09:59 被阅读0次

    别名,便于书写。

    整型:

    typedef int ZHANGSAN;  //为 int 多取一个名字,ZHANGSAN 等价于 int
    
    int main(void) 
    {
        int i = 10;// 等价于 ZHANGSAN = 10
        ZHANGSAN j = 20;
        printf("%d",j);  //20
    
        return 0;
    }
    

    结构体:

    typedef struct Student
    {
        int sid;
        char name[20];
    }ST;
    
    int main(void) 
    {
        struct Student st; //等价于 ST st
        struct Student * pst;  //等价于 ST *pst
    
        ST st2;
        st2.sid = 200;
        printf("%d",st2.sid);  //200
        return 0;
    }
    
    
    typedef struct Student
    {
        int sid;
        char name[20];
    } * PST;  //PST 等价于 struct Student *
    
    int main(void) 
    {
        struct Student st;
        PST ps = &st; //等价于 struct Student * ps = &st
        ps->sid = 99;
        printf("%d",ps->sid);  //99
    
        return 0;
    }
    
    
    typedef struct Student
    {
        int sid;
        char name[20];
    } * PST,ST;  //PST 等价于 struct Student *,ST 等价于 struct Student
    
    int main(void) 
    {
        ST st;  //等价于 struct Student st
        PST ps = &st;  //等价于 struct Student * ps = &st
        ps->sid = 99;
        printf("%d",ps->sid);  //99
    
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:typedef

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