美文网首页
C语言-struct、typedef struct的区别

C语言-struct、typedef struct的区别

作者: 阿阿G讷 | 来源:发表于2020-04-04 17:47 被阅读0次


#include <stdio.h>


typedef struct Test3 
{
    int a;
    int* p;
    struct Test* k;//k是指向‘结构体类型为TEST’的结构体变量的指针
}Test3;//struct Test3 = Test3,用这两个都可以定义结构体变量

typedef struct
{
    int a;
    int* p;
    struct Test* k;//定义一个指向‘结构体变量’的指针
}Test4;

struct Test           //Test是结构体类型名字!!注意了!!
{
    int a;
    int* p;
    struct Test* k;
}test1, test2; //此处test1,test2是结构体变量!!

struct              //省略了结构体类型名字
{
    int a;
    int* p;
    struct Test* k;
}Test2;//经过测试 :此处Test2是结构体变量!!能直接拿来用!省不省略Test是一样的!!

void typedef_strcut_test1()
{
    struct Test3 test3;//尝试定义test3结构体变量
    Test3 test4;//尝试定义test4结构体变量,test3和test4是一样的
    test3.a = 6;
    test3.p = &test3.a;
    //test3.k = &test4写法是错误的,原因:理解:他们的指向结构体变量的指针,都是只能指向Test型的结构体变量
}


相关文章

网友评论

      本文标题:C语言-struct、typedef struct的区别

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