结构体,联合体

1.结构体
     ①结构体的定义:

struct fruit
{
   char name[20];
   char color[20];
   float price;
};

    ②结构体创建变量:

struct fruit
{
   char name[20];
   char color[20];
   float price;
}apple, balana;

或者

struct fruit
{
   char name[20];
   char color[20];
   float price;
};
fruit apple;

    ③结构体的初始化:

struct fruit
{
   char name[20];
   char color[20];
   float price;
};
fruit apple = 
{
    "apple",
    "red",
    19.99
};
或者

struct fruit
{
   char name[20];
   char color[20];
   float price;
} apple = 
{
    "apple",
    "red",
    19.99
};
或者

struct fruit
{
   char name[20];
   char color[20];
   float price;
} apple = 
{
    "apple",
    "red",
    19.99
};
fruit apple_new = apple;

 ④结构体程序实例:

#include <iostream>

using namespace std;

struct fruit
{
    char name[20];
    char color[20];
    float price;
};
int main()
{
    fruit apple =
    {
        "apple",
        "red",
        19.99
    };
    fruit pear =
    {
        "pear",
        "yellow",
        20.99
    };
    cout << "the name of the first fruit is " << apple.name << endl;
    cout << "the total price of the two fruit is " << apple.price + pear.price << endl;
    return 0;
}

2.联合体跟结构体使用情况类似,唯一不同的是联合体所有的属性占用同一块内存空    间,所以联合体所占用的内存      的大小是所有属性中的占用最大的那个。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值