1.结构变量
C语言中,提供了**结构变量(structure variable)**用于提高我们表示数据的能力,如果我们要打印一本书的图书目录,其中包含书名作者等等信息,这样我们需要这种数据形式既能包含字符串,又能包含数字,而且各信息独立,此时我们就可以使用结构变量来储存数据。
1.1 结构变量声明
**结构声明(structure declaration)**描述了一个结构的组织布局,如下所示:
struct book { //结构模板:标记是book
char title[MAXTITL];
char author[MAXAUTL];
float value;
}; //结构模板结束
该声明描述了一个由两个字符数组和一个float类型变量组成的结构。
struct book library; //把library声明为一个使用book结构布局的结构变量
1.2 初始化结构
初始化一个结构变量与初始化数组的语法类似:
struct book library = {
"The Pious Pirate and the Devious Damsel",
"Renee Vivotee",
1.95
};
1.3 访问结构成员
使用结构成员运算符-点(.)访问结构中的成员。如
library.value //访问library的value部分
本质上,.title、.author、.value的作用相当于book的下表。
1.4 实例
采用结构变量存储一本书的目录,并显示:
//一本图书的目录
#include <stdio.h>
#include <string.h>
char * s_gets(char * st, int n);
#define MAXTITL 41 //书名的最大长度+1
#define MAXAUTL 31 //作者姓名的最大长度+1
struct book { //结构模板:标记是book
char title[MAXTITL];
char author[MAXAUTL];
float value;
}; //结构模板结束
int main(void)
{
struct book library; //把library声明为一个book类型的变量
printf("please enter the book title.\n");
s_gets(library.title, MAXTITL);
printf("enter the author.\n");
s_gets(library.author, MAXAUTL);
printf("now enter the value.\n");
scanf("%f", &library.value);
printf("%s by %s : $%.2f\n", library.title, library.author, library.value);
printf("Done.\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;
}
运行结果:
2. 结构数组
当上个例子要处理多本书时,显然此时每本书都可用一个book类型的结构变量来表示,当使用多个结构数组来处理多本书是,就可以使用如下程序所示的数组。
//包含多本书的图书目录
#include <stdio.h>
#include <string.h>
char * s_gets(char * st, int n);
#define MAXTITL 40 //书名的最大长度+1
#define MAXAUTL 40 //作者姓名的最大长度+1
#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("Please enter the book title.\n");
while(count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0')
{
printf("enter the author.\n");
s_gets(library[count].author, MAXAUTL); //获取输入的作者名,并存入结构数组
printf("now enter the value.\n");
scanf("%f", &library[count++].value);
while(getchar() != '\n')
continue; //清理输入行
if (count < MAXBKS)
printf("enter the next title.\n");
}
if (count > 0)
{
printf("here is the list of your books:\n");
for (index = 0; index < count; index++)
printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
}
else
printf("No books?Too bad.\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;
}
运行结果:
2.1 声明结构数组
声明结构数组和声明其他类型的数组类似,如下
struct book library[MAXBKS]; //library为一个内含MAXBKS个元素的数组
数组中的每个元素都是一个book类型的数组。
2.2 标识结构数组的成员
2.3 嵌套结构
在一个结构中包含另一个结构即嵌套结构。如,我们想创建一个有关自己朋友信息的结构,显然,结构中需要一个成员表示朋友的名字,此时,名字可以用数组表示,包含名和姓两个成员。
例子程序:
//嵌套结构示例
#include <stdio.h>
#define LEN 20
const char * msgs[5] =
{
"thank your for the wonderful evening,",
"You certainly prove that a",
"is a special kind of guy. We must get together",
"over a delicious",
" and have a few laughs"
};
struct names{ //第一个结构
char first[LEN];
char last[LEN];
};
struct guy{ //第2个结构
struct names handle; //嵌套结构
char favfood[LEN];
char job[LEN];
float income;
};
int main(void)
{
struct guy fellow = { //初始化一个结构变量
{ "Ewen", "Villard"},
"grilled salmon",
"personality coach",
68112.00
};
printf("Dear %s, \n\n", fellow.handle.first);
printf("%s%s.\n", msgs[0], fellow.handle.first);
printf("%s%s\n", msgs[1], fellow.job);
printf("%s\n", msgs[2]);
printf("%s%s%s", msgs[3], fellow.favfood, msgs[4]);
if (fellow.income > 150000.0)
puts("!!");
else if (fellow.income > 75000.0)
puts("!");
else
puts(".");
printf("\n%40s%s\n", " ", "See you soon,");
printf("%40s%s\n", " ", "Shalala");
return 0;
}
运行结果: