结构体数组与结构体指针的学习心得

#include<stdio.h>
typedef struct
{
    int num;
    char name[20];
    float score;
}STU;
int main()
{
    STU edu[3]={
            {101,"lucy",78},
            {102,"bob",59.5},
            {103,"Tom",85}
    };
    int i;
    float sum=0;
    for(i=0;i<3;i++)
    {
        sum +=edu[i].score;
        printf("Average score is %.2lf\n",sum / 3);
    }
}

结构体的数组区别于普通的结构体定义,在

于struct xxx[]后有中括号

本文使用的定义方法为typedef 即定义其为STU

在后期的数组调用时候,只需要 STU edu[3]

这段代码展示了于结构体STU定义结构体数组edu的过程

在后期我们调用edu这一STU类型的结构体数组时,只使用edu[i].score这样的方式调用

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu{
    int id;
    char name[32];
    char sex;
    int age;
};
int main()
{
    struct stu *s;
    s=(struct stu *) malloc(sizeof(struct stu));
    s->id=1001;
    strcpy(s->name,"张三");
    s->sex='b';
    s->age=20;
    printf("%d %s %c %d\n",s->age,s->name,s->sex,s->id);

}

———————————————————————————————————————————

分析上面的代码 我们得出了结论

在结构体的指针形式中

我们可以用结构体名->结构体部分的方式定义

例如s->name 这里就是s这个指针变量指向了name这个结构体的部分

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值