C 语言教程 在线

1524C 共用体

结构体与共用体

结构体变量所占内存长度是各成员占的内存长度之和。每个成员分别占有其自己的内存单元。

共用体变量所占的内存长度等于最长的成员变量的长度。例如,教程中定义的共用体Data各占20个字节(因为char str[20]变量占20个字节),而不是各占4+4+20=28个字节。

union Data
{
   int i;
   float f;
   char  str[20];
} data;  

1523C 结构体

使用结构数组存储书名/作者,结构体指针访问成员时,也可以对指针解引用再访问,如:*struct_pointer.title;(上面范例)。

#include<stdio.h>
#include<string.h>

char * s_gets(char*st, int n);

#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100  // 书籍的最大数量

struct book {   //简历 book 模板
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

int main(void)
{
    struct book library[MAXBKS];  //book 结构类型数组
    int count = 0;
    int index;
    printf("请输入书名:\n");
    printf("按下 [enter] 键结束输入。\n");
    while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0')
    {
        printf("请输入作者:\n");
        s_gets(library[count].author, MAXAUTL);
        printf("请输入价格:\n");
        scanf("%f", &library[count++].value);
        while (getchar() != '\n')
            continue;  //清理输入行
        if (count < MAXBKS)
            printf("输入下一本书。\n");
    }
    if (count > 0)  // 如果数组内有存书籍
    {
        printf("书的列表:\n");
        for (index = 0; index < count; index++)  // 遍历已存入的书籍,
            printf("%s - %s:$%.2f\n", library[index].title, library[index].author, library[index].value); // 将内容打印出来
    }
    else
        printf("没有书。\n");  // 否则就打印没书
    return 0;
}

char * s_gets(char * st, int n)  //输入文本(作家)函数
{
    char * ret_val;
    char * find;
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');  //查找换行符
        if (find)  //如果地址不是NULL
            *find = '\0';  //在此处放置一个空字符
        else
            while (getchar() != '\n')
                continue;
    }
    return ret_val;
}

1522C 结构体

结构体数组

一个结构体变量中可以存放一组数据(如一个学生的学号,姓名,成绩等数据)。如果有10个学生的数据需要参加运算,显然应该用数组,这就是结构体数组。结构体数组与以前介绍过的数据值型数组不同之处在于每个数组元素都一个结构体类型的数据,它们分别包括各个成员(分量)项。

定义结构体数组

和定义结构体变量的方法相仿,只需说明其为数组即可。

struct student
{
    int num;
    char name[20];
    char sex;
    int age;
    float score;
    char addr[30];
};
struct student stu[3];

以上定义了一个数组 stu,其元素为 struct student 类型数据,数组有 3 个元素。也可以直接定义一个结构体数组。如:

struct student
{
    int num;
    ....

}stu[3];

或

struct

{
    int num;
     ...
}stu[3];

结构体数组的初始化

与其它类型数组一样,对结构体数组可以初始化如:

struct student
{
    int mum;
    char name[20];
    char sex;
    int age;
    float score;
    char addr[30];
}stu[3] = {{10101,"Li Lin", 'M', 18, 87.5, "103 Beijing Road"},
            {10101,"Li Lin", 'M', 18, 87.5, "103 Beijing Road"},
            {10101,"Li Lin", 'M', 18, 87.5, "103 Beijing Road"}};

定义数组 stu 时,元素个数可以不指定,即写成以下形式:

stu[] = {{...},{...},{...}};

编译时,系统会根据给出初值的结构体常量的个数来确定数组元素的个数。

当然,数组的初始化也可以用以下形式:

struct student
{
    int num;
    ...
};
struct student stu[] = {{...},{...},{...}};

即先声明结构体类型,然后定义数组为该结构体类型,在定义数组时初始化。

从以上可以看到,结构体数组初始化的一般形式是在定义数组的后面加上:

结构体数组应用举例

下面例子说明结构体数组的定义和引用。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct person
{
    char name[20];
    int count;

}leader[3] = {{"Li", 0},
             {"Zhang", 0},
             {"Fun", 0}};

void main()
{
    int i, j;
    char leader_name[20];
    for(i = 1; i<= 10;i++)
    {
        scanf("%s", leader_name);
        for(j=0;j<3;j++)
            if(strcmp(leader_name, leader[j].name) == 0)
                leader[j].count ++;
    }
    printf("\n");
    for(i=0;i<3;i++)
        printf("%5s: %d\n", leader[i].name, leader[i].count);
    system("pause");
}

运行结果如下:

LI
Li
Fun
Zhang
Zhang
Fun
Li
Fun
Zhang
Li

   Li: 3
Zhang: 3
  Fun: 3

更多内容参考:C 结构体详解

1521C 结构体

可以在声明结构体时初始化结构体变量:

#include <stdio.h>

int main(void)
{
    struct Student {
        char name[50];
        int gender;
        int age;
    } student2 = {"张三",0,30};
    struct Student student1;
    printf("name:\n");
    scanf("%s",student1.name);
    printf("gender:\n");
    scanf("%d",&student1.gender);
    printf("age:\n");
    scanf("%d",&student1.age);
    printf("student1 >>name = %s, gender = %d, age = %d\n", student1.name, student1.gender, student1.age);
    printf("student2 >>name = %s, gender = %d, age = %d\n", student2.name, student2.gender, student2.age);
}

1520C 结构体

结构体数组:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXTITL 41
#define MAXAUTL 31
#define MAVXBKS 100

char * s_gets(char * st, int n);
struct book {
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

int main()
{
    struct book library[MAVXBKS];    //book类型的结构体数组
    int i;
    int index;
    printf("请问你要录入多少本书的信息\n");
    do
    {
        scanf("%d", &index);
    } while (index > MAVXBKS);
    getchar();
    for (i = 0; i < index; i++)
        {
        printf("请输入第%d本书的名称:\n",i+1);
        s_gets(library[i].title, MAXTITL);
        printf("输入其作者的名字:\n");
        s_gets(library[i].author, MAXAUTL);
        printf("请输入书本的价格:\n");
        scanf("%f", &library[i].value);
        getchar();
    }
    for (i = 0; i < index; i++)
    {
            printf("%d\t%s  是  %s 写的 定价为%f元\n", i,library[i].title, library[i].author, library[i].value);
    }
    system("pause");
    return 0;
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    char * find;
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');    //查找换行符
        if (find)    //  查找地址不为空
            *find = '\0';    //在此处放入一个空字符
        else
            while (getchar() != '\n')
        continue;    //处理剩余字符
    }
    return ret_val;
}