博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[编程] C语言的结构体
阅读量:6968 次
发布时间:2019-06-27

本文共 2365 字,大约阅读时间需要 7 分钟。

结构体

  struct 结构体名{} 变量名;

结构体变量:

        struct person{

                char *name;

                int age;

                float score;

        } student;

成员的获取和赋值

        //Members of the acquisition and assignment

        student.name="taoshihan";

        student.age=30;

        student.score=100;

        printf("name=%s \n",student.name);

C语言结构体数组

        struct stu{

                char *name;

                int age;

                float score;

        } classes[5];

遍历结构体数组

        struct people{

                char *name;

                int age;

                float score;

        } d[]={

                {"taoshihan",20,100},

                {"lisi",30,90}

        };

        int len=sizeof(d)/sizeof(d[0]);

        printf("d length=%d \n",len);

        for(int i=0;i<len;i++){

                printf("loop...%s,%d,%.1f \n",d[i].name,d[i].age,d[i].score);

        }

C语言结构体和指针

  struct 结构体名 *变量名;

        struct person1{

                char *name;

                int age;

                float score;

        } a={"taoshihan",20,100},*b=&a;

        struct person1 *c=&a;

获取结构体成员

        printf("b.name=%s \n",(*b).name);

        printf("c.name=%s \n",c->name);

完整代码:

#include 
int main(){ printf("hello world"); //Structure variables struct person{ char *name; int age; float score; } student; //Members of the acquisition and assignment student.name="taoshihan"; student.age=30; student.score=100; printf("name=%s \n",student.name); //c struct array struct stu{ char *name; int age; float score; } classes[5]; struct stu1{ char *name; int age; float score; } classes1[2]={ {
"taoshihan",20,100.00}, {
"lisi",20,90} }; struct stu2{ char *name; int age; float score; } classes3[]={ {
"taoshihan",20,100} }; printf("%s \n",classes1[1].name); //Traverse the array of structures struct people{"chaper5.c" 71L, 1199C 1,1 Top //Traverse the array of structures struct people{ char *name; int age; float score; } d[]={ {
"taoshihan",20,100}, {
"lisi",30,90} }; int len=sizeof(d)/sizeof(d[0]); printf("d length=%d \n",len); for(int i=0;i
name);}

 

转载于:https://www.cnblogs.com/taoshihan/p/7856226.html

你可能感兴趣的文章
A Simple Problem with Integers(线段树入门题)
查看>>
福大软工 · 第七次作业 - 需求分析报告
查看>>
用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 操作符重载和隐式类型转换
查看>>
腾讯云入门
查看>>
20165226 2017-2018-4 《Java程序设计》第6周学习总结
查看>>
linux网络配置命令(二)——ip
查看>>
泛型通配符extends与super的区别
查看>>
openpose模型在AI challenge人体骨骼关键点检测的表现
查看>>
==与equals
查看>>
D2.Reactjs 操作事件、状态改变、路由
查看>>
初探 插头DP
查看>>
bzoj 2244: [SDOI2011]拦截导弹
查看>>
UNIX 系统概述
查看>>
Ubuntu14.04下安装Hadoop2.4.0 (单机模式)
查看>>
每周一荐:Google的序列化框架Protobuf
查看>>
002-B/S架构&C/S架构
查看>>
iOS注册collcetionViewFlowLayout
查看>>
python-selenium 元素定位
查看>>
windows下python的安装
查看>>
解决数据库卡、慢,问题多,难管理——老技术的执著
查看>>