结构体类型

如果需要用代码实现对多组数据的分别计算

可以用结构体,将同一组数据包装在一起

一、先定义后声明:
  1.定义:
struct 结构体名{
组成1
组成2
......
};

  2.声明:
struct 结构体名 第一个结构成员名(包括组成1、组成2、...)
struct 结构体名 第二个结构成员名(包括组成1、组成2、...)
...

例如:
struct teacher{
    char name[20];
    int age;
    char email[50];
};

struct teacher teacher1;
struct teacher teacher1;(定义)

或者
struct teacher teacher1={
   "liuyichen";
   18;
   "xx@qq.com";
};                       (定义并初始化)

二、定义的同时声明:

struct teacher{
    char name[20];
    int age;
    char email[50];
}teacher1, teacher2;(没有初始化)

三、使用typedef定义结构体:

typedef struct Student
{
    char name[50];
    int age;
}Stu;//这一步仅是将struct Student 改名为Stu,而没有声明,以上仅是定义

不需要再struct Student student1,只需要Stu student1
利用typedef相当于把类型名 struct Student 自定义为了Stu


2.结构体变量的赋值

typedef struct
{
    int a, b, c, d, e;
} x;

scanf("%d%d%d%d%d", &x[i].a, &x[i].b, &x[i].c, &x[i].d, &x[i].e);
struct Person {
    char name[20];
    int age;
};

int main() {
    struct Person p;
    p.age = 25;
    strcpy(p.name, "Alice");

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值