结构体

1:什么是结构体

把一些复杂的数据组合在一起形成新的复合数据类型,叫做结构体。

2:为什么要有结构体?

为了表示一些复杂的事物,而普通的基本类型无法满足实际的应用需求。

3:结构体的基本格式

struct 结构名称
    {
        结构体参数列表;
    }
比较好的应用方式就是将结构体变量定义在函数中,使程序更容易移植,耦合性更低。

4:怎么定义一个结构体和结构体的3种格式

/* 第一种方式:定义一个结构体*/
/* 这种方式应用更多*/
struct comm
    {
        int heard;
        int lenth;
        int addmast;
        int addslav;
        int ctr;
        int end;
        int crc;
    }
/* 第二种方式:定义一个结构体*/
/* 定义结构体同时定义结构体变量*/
struct comm
    {
        int heard;
        int lenth;
        int addmast;
        int addslav;
        int ctr;
        int end;
        int crc;
    }commucation;
/* 第二种方式:定义一个结构体*/
/* 定义结构体同时定义结构体变量,但是不定义结构体名称*/
struct
    {
        int heard;
        int lenth;
        int addmast;
        int addslav;
        int ctr;
        int end;
        int crc;
    }commucation;

5:结构体程序范例

/*普通结构体定义,初始化,赋值*/
# include<stdio.h>

struct comm
{
    int heard;
    int add;
    int ctr;
    int crc;
};

int main()
{
    struct comm test;//结构体初始化
    test.heard = 1;//结构体赋值
    test.add  = 2;
    test.ctr  = 3;
    test.crc  = 4;

    printf("%d,%d,%d,%d\n",test.heard,test.add,test.ctr, test.crc);

    return 0;
}
/*普通结构体定义,初始化,赋值*/
# include<stdio.h>

struct comm
{
    int heard;
    int add;
    int ctr;
    int crc;

};

int main()
{
     struct comm test ;//定义结构体变量 
     struct comm * p = &test;//定义指向结构体变量的指针变量

     p ->heard = 1;//结构体变量的赋值
     p -> add  = 2;
     p -> ctr  = 3;
     p -> crc  = 4;

     printf("%d,%d,%d,%d\n",p -> heard,p -> add, p -> ctr, p -> crc);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值