学习笔记—C语言基础篇08

来源:互联网 发布:安卓锁机软件制作教程 编辑:程序博客网 时间:2024/06/17 13:46
一、结构体(struct)
typedef struct Stu //结构体名称
{
     //结构体成员;
}stu;
结构体数组#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct Student{    char name[21];    int scores[3];}stu;int main05(){    stu s[2];    printf("结构体大小:%d\n", sizeof(s[0]));    printf("结构体数组大小:%d\n", sizeof(s));    for (int i = 0; i < sizeof(s) / sizeof(s[0]); i++)    {        //strcpy(s[i].name, "蒙奇·D·王老七");        scanf("%s",s[i].name);        for (int j = 0; j < 3; j++)        {            scanf("%d", &s[i].scores[j]);        }        //s[i].scores = 100;    }    for (int i = 0; i < 2; i++)    {        printf("姓名:%s    ", s[i].name);        for (int j = 0; j < 3; j++)        {            printf("成绩:%d  ", s[i].scores[j]);        }    }    system("pause");    return EXIT_SUCCESS;}结构体指针#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct Student{    char * name;//4    int * scores;//4}stu;int main07(){    //结构体指针    stu * st = (stu *)malloc(sizeof(stu));    st->name = (char *)malloc(sizeof(char) * 21);    st->scores = (int *)malloc(sizeof(int) * 3);    strcpy(st->name, "张学友");    st->scores[0] = 100;    st->scores[1] = 200;    st->scores[2] = 300;    printf("姓名:%s\n成绩1:%d\n成绩2:%d\n成绩3:%d\n",        st->name, st->scores[0], st->scores[1], st->scores[2]);    free(st->name);    free(st->scores);    free(st);    return EXIT_SUCCESS;}结构体和指针#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct Student{    char * name;    //int scores[3];    char * scores;}stu;int main06(){    stu s[2];    memset(s, 0, sizeof(s));    for (int i = 0; i < 2; i++)    {        //开辟堆空间 存放姓名和成绩        s[i].name = (char *)malloc(sizeof(char) * 21);        memset(s[i].name, 0, 21);        s[i].scores = (int*)malloc(sizeof(int) * 3);        memset(s[i].scores, 0, 12);        //赋值        scanf("%s", s[i].name);        for (int j = 0; j < 3; j++)        {            scanf("%d", &s[i].scores[j]);        }    }    //打印    for (int i = 0; i < 2; i++)    {        printf("姓名:%s    ", s[i].name);        for (int j = 0; j < 3; j++)        {            printf("成绩:%d  ", s[i].scores[j]);        }    }    //释放    for (int i = 0; i < 2; i++)    {        free(s[i].name);        free(s[i].scores);    }    return EXIT_SUCCESS;}结构体指针数组#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct Student{    char * name;    int * scores;}stu;int main(){    stu * st[3];    for (int i = 0; i < 3; i++)    {        st[i] = (stu *)malloc(sizeof(stu));        st[i]->name =(char *) malloc(sizeof(char) * 21);        st[i]->scores = (int *)malloc(sizeof(int) * 3);        scanf("%s", st[i]->name);        for (int j = 0; j < 3; j++)        {            scanf("%d", &st[i]->scores[j]);        }    }    for (int i = 0; i < 3; i++)    {        printf("姓名:%s   ", st[i]->name);        for (int j = 0; j < 3; j++)        {            printf("成绩:%d", st[i]->scores[j]);        }        printf("\n");    }    //释放    for (int i = 0; i < 3; i++)    {        free(st[i]->name);        free(st[i]->scores);        free(st[i]);    }    return EXIT_SUCCESS;}结构体冒泡排序#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct Student{    char * name;    int * scores;//存储三门成绩 根据总成绩排序}stu;void BubbleSort(stu * st, int len){    for (int i = 0; i < len - 1; i++)    {        for (int j = 0; j < len - 1 - i; j++)        {            //判断 score【0】+score【1】+score【2】            int sum1 = st[j].scores[0] + st[j].scores[1] + st[j].scores[2];            int sum2 = st[j + 1].scores[0] + st[j + 1].scores[1] + st[j + 1].scores[2];            if (sum1 > sum2)            {                //交换的是结构体成员中的指针变量                stu temp = st[j];                st[j] = st[j + 1];                st[j + 1] = temp;            }        }    }}int main(){    stu st[5];    for (int i = 0; i < 5; i++)    {        st[i].name = (char *)malloc(sizeof(char) * 21);        st[i].scores = (int *)malloc(sizeof(int) * 3);        scanf("%s", st[i].name);        for (int j = 0; j < 3; j++)        {            scanf("%d", &st[i].scores[j]);        }    }    BubbleSort(st, 5);    for (int i = 0; i < 5; i++)    {        printf("姓名:%s  ", st[i].name);        for (int j = 0; j < 3; j++)        {            printf("成绩:%d  ", st[i].scores[j]);        }        printf("\n");    }    for (int i = 0; i < 5; i++)    {        free(st[i].name);        free(st[i].scores);    }    return EXIT_SUCCESS;}
二、共用体(union)
共用体union是一个能在同一个存储空间存储不同类型数据的类型;
联合体所占的内存长度等于其最长成员的长度,也有叫做共用体;
同一内存段可以用来存放几种不同类型的成员,但每一瞬时只有一种起作用;
共用体变量中起作用的成员是最后一次存放的成员,在存入一个新的成员后原有的成员的值会被覆盖;
共用体变量的地址和它的各成员的地址都是同一地址。
三、枚举(enum)
一般配合switch使用
将变量的值一一列举出来,变量的值只限于列举出来的值的范围内。
在枚举值表中应列出所有可用值,也称为枚举元素。
枚举值是常量,不能在程序中用赋值语句再对它赋值。
枚举元素本身由系统定义了一个表示序号的数值从0开始顺序定义为0,1,2 …

0 0