前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >[编程] C语言的结构体

[编程] C语言的结构体

作者头像
唯一Chat
发布2019-09-10 19:32:29
发布2019-09-10 19:32:29
2.2K00
代码可运行
举报
文章被收录于专栏:陶士涵的菜地陶士涵的菜地
运行总次数:0
代码可运行

结构体

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);

完整代码:

代码语言:javascript
代码运行次数:0
复制
#include <stdio.h>
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<len;i++){
                printf("loop...%s,%d,%.1f \n",d[i].name,d[i].age,d[i].score);
        }

        //C language structure and pointer
        struct person1{
                char *name;
                int age;
                float score;
        } a={"taoshihan",20,100},*b=&a;
        struct person1 *c=&a;
        //Get the structure member

        printf("b.name=%s \n",(*b).name);
        printf("c.name=%s \n",c->name);
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-11-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档