typedef

2021-07-25  本文已影响0人  Vergil_wj

别名,便于书写。

整型:

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;
}

上一篇下一篇

猜你喜欢

热点阅读