ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

chapter 7 结构体&共用体

2021-11-22 01:03:48  阅读:154  来源: 互联网

标签:chapter name int stu score1 score2 score3 共用 结构


目录

大学C语言程序设计

chapter 7 结构体

1. 结构体的认识

结构体概念:可以把各种类型的数据放在一起,形成新的数据类型。
如何定义:关键字 struct
格式:

struct 结构名{
    数据类型 变量名1;
    数据类型 变量名2;
};

如:定义一个学生,他有姓名,年龄,语文成绩,数学成绩,英语成绩

struct student{
    char name[20];
    int age;
    int score1,score2,score3;
};

访问结构体中的信息,通过 ”.”来访问

student stu;  //定义一个结构体变量 stu
strcpy(stu.name,"helloA");   //字符数组的特殊赋值方式
stu.age=13;
stu.score1=88;
stu.score2=99;
stu.score3=100;
printf("%s %d %d %d\n",stu.name,stu.age,stu.score1,stu.score2,stu.score3);//输出

结构体的定义形式

//方式 1
struct student{
    char name[20];
    int age;
    int score1,score2,score3;
}stu1,stu2;

//方式 2
student stu3,stu4;

结构体初始化赋值

struct student{
    char name[20];
    int age;
    int score1,score2,score3;
}stu={"helloB", 14,99,100,100};//初始化赋值

结构体数组

struct student{
    char name[20];
    int age;
    int score1,score2,score3;
}stu[N];

或者:student stu[N];

结构体数组初始化赋值
方式一

#include<stdio.h>
#include<string.h>
struct student{
    char name[20];
    int age;
    int score1,score2,score3;
}stu[3]={
{"helloA",13,88,99,100},
{"helloB",14,90,99,100},
{"helloC",15,100,100,100}};

int main(){
    for(int i=0; i<3; i++){
        printf("%s %d %d %d\n",stu[i].name,stu[i].age,stu[i].score1,stu[i].score2,stu[i].score3);
    }
    return 0;
}

方式二

#include<stdio.h>
#include<string.h>
struct student{
    char name[20];
    int age;
    int score1,score2,score3;
};

struct student stu[3]={{"helloA",13,88,99,100},{"helloB",14,90,99,100},{"helloC",15,100,100,100}};
int main(){
    for(int i=0; i<3; i++){
        printf("%s %d %d %d\n",stu[i].name,stu[i].age,stu[i].score1,stu[i].score2,stu[i].score3);
    }
    return 0;
}

2. 结构体信息输入输出

单个结构体信息输入输出

#include<stdio.h>
#include<string.h>
struct student{
    char name[20];
    int age;
    int score1,score2,score3;
}stu;

int main(){
   char name[20];
    scanf("%s %d%d%d%d", name, &stu.age, &stu.score1,&stu.score2,&stu.score3);
    strcpy(stu.name, name);
    printf("%s %d %d %d %d\n", stu.name,stu.age,stu.score1,stu.score2,stu.score3);
    return 0;
}

多个结构体信息输入输出

#include<stdio.h>
#include<string.h>
#define N 1000
struct student{
    char name[20];
    int age;
    int score1,score2,score3;
}stu[N];

int main(){
    char name[20];
    int n; scanf("%d", &n);
    for(int i=0; i<n; i++){
        scanf("%s %d%d%d%d", name, &stu[i].age, &stu[i].score1,&stu[i].score2,&stu[i].score3);
        strcpy(stu[i].name, name);
    }
    for(int i=0; i<n; i++){
        printf("%s %d %d %d %d\n", stu[i].name,stu[i].age,stu[i].score1,stu[i].score2,stu[i].score3);
    }
    return 0;
}

3. 结构体成员函数

struct student{
    char name[20];
    int age;
    int score1,score2,score3;
    int sum(){
        return score1+score2+score3; //求得成绩总和
    }
}stu[N];

printf("sum()=%d\n", stu[i].sum()); //使用方法

4. 结构体排序

5. 结构体指针

6. 链表

7. 共用体类型

8. 枚举类型

9. typedef声明新类型名

标签:chapter,name,int,stu,score1,score2,score3,共用,结构
来源: https://www.cnblogs.com/hellohebin/p/15456893.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有