结构体
结构体的形式
struct 结构体名称{
结构体成员1;
结构体成员2;
…
};
例如: 图书的结构体
struct Book{char title[128];char author[48];float price;unsigned int date;char publisher[40];};
定义结构体类型变量
可以在结构体声明时 后面跟上变量,这个变量是全局变量
例如
struct Book{char title[128];char author[48];float price;unsigned int date;char publisher[40];}book;//boo是全局变量
也可以在主函数中定义
#include <stdio.h>struct Book{char title[128];char author[48];float price;unsigned int date;char publisher[40];};int main(void){struct Book book;//这个是局部变量return 0;}
访问结构体变量
要访问结构体成员,我们需要引入一个新的运算符 —点号(.)运算符。比如book.title就是引用book结构体的title成员, 他是一个字符数组;而book.price 则是引用book结构体的price成员,它是一个浮点型的变量
例如 图书
#include <stdio.h>struct Book{char title[128];char author[48];float price;unsigned int date;char publisher[40];}book;int main(void){printf("请输入书名: ");scanf("%s", book.title);printf("请输入作者: ");scanf("%s", book.author);printf("请输入售价: ");scanf("%f", &book.price);printf("请输入出版日期: ");scanf("%d", &book.date);printf("请输入出版社: ");scanf("%s", book.publisher);printf("\n=======数据录入完毕=====\n");printf("书名: %s\n", book.title);printf("作者: %s\n", book.author);printf("售价: %.2f\n", book.price);printf("出版日期: %d\n", book.date);printf("出版社: %s\n", book.publisher);return 0;}
结果输出
请输入书名: 老人与海请输入作者: 海明威请输入售价: 20请输入出版日期: 0113请输入出版社: 中国矿业大学出版社=======数据录入完毕=====书名: 老人与海作者: 海明威售价: 20.00出版日期: 0113出版社: 中国矿业大学出版社
初始化结构体变量
初始化一个结构体变量:
struct Book book = {"老人与海","海明威";20,0113"中国矿业大学出版社"};
一定要对号入座
初始化结构体的指定成员值
结构体初始化成员使用点号(.)运算符和成员名
例如:让程序只初始化Book的price成员:
struct Book book = {.price = 20};
也可以不按结构体声明的成员顺序进行初始化:
struct Book book = {.publisher = "中国矿业大学出版社",.price = 20,.date = 0113};
结构体嵌套
嵌套例子:
#include <stdio.h>struct Date{int year;int month;int day;};struct Book{char title[128];char author[40];float price;struct Date date;//嵌套char publisher[40];}book = {"老人与海","海明威",20,{, 01, 13},"中国矿业大学出版社"};int main(void){printf("书名: %s\n", book.title);printf("作者: %s\n", book.author);printf("价格: %.2f\n", book.price);printf("日期: %d-%d-%d\n", book.date.year, book.date.month,book.date.day);printf("出版社: %s\n", book.publisher);return 0;}
输出结果
书名: 老人与海作者: 海明威价格: 20.00日期: -1-13出版社: 中国矿业大学出版社
结构体数组
第一种方法是在声明结构体的时候进行定义:
struct 结构体名称{结构体成员1;结构体成员2;...}数组名[长度];
第二种方法是先声明一个结构体类型,在用此类型定义一个结构体数组:
struct 结构体名称{结构体成员1;结构体成员2;...};struct 结构体名称 数组名[长度];
初始化结构体数组
例子:
struct Book book[3] = {{"老人与海", "海明威", 20, {, 01, 13}, "中国矿业大学出版社"};{"老人与陆", "陆明威", 21, {, 01, 13}, "清华大学出版社"};{"老人与空", "空明威", 22, {, 01, 13}, "北京大学出版社"};};
传递结构体变量
结构体变量之间可以通过赋值号传递信息,前提是变量的类型相同;例子:
#include <stdio.h>int main(void){struct Test{int x;int y;}t1, t2;t1.x = 3;t1.y = 4;t2 = t1;//赋值号传递printf("t2.x = %d, t2.y = %d", t2.x, t2.y);return 0;}
输出结果
t2.x = 3, t2.y = 4
结构体指针
例子:图书录入程序
#include <stdio.h>struct Date{int year;int month;int day;};struct Book{char title[128];char author[40];float price;struct Date date;char publisher[40];};struct Book getInput(struct Book book);void printBook(struct Book book);struct Book getInput(struct Book book){printf("请输入书名: ");scanf("%s", book.title);printf("请输入作者: ");scanf("%s", book.author);printf("请输入售价: ");scanf("%f", &book.price);printf("请输入出版日期: ");scanf("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);printf("请输入出版社: ");scanf("%s", book.publisher);return book;};void printBook(struct Book book){printf("书名: %s\n", book.title);printf("作者: %s\n", book.author);printf("售价: %.2f\n", book.price);printf("出版日期: %d-%d-%d\n", book.date.year,book.date.month,book.date.day);printf("出版社: %s\n", book.publisher);}int main(void){struct Book b1, b2;printf("请录入第一本书的信息...\n");b1 = getInput(b1);putchar('\n');printf("请录入第二本书的信息...\n");b2 = getInput(b2);printf("\n\n录入完毕,现在开始打印验证...\n\n");printf("打印第一本书的信息..\n");printBook(b1);putchar('\n');printf("打印第二本书的信息..\n");printBook(b2);return 0;}
输出结果
请录入第一本书的信息...请输入书名: 老人与海请输入作者: 海明威请输入售价: 20请输入出版日期: -1-13请输入出版社: 清华大学出版社请录入第二本书的信息...请输入书名: 年轻人与海请输入作者: 海明威请输入售价: 20请输入出版日期: -1-13请输入出版社: 北京大学出版社录入完毕,现在开始打印验证...打印第一本书的信息..书名: 老人与海作者: 海明威售价: 20.00出版日期: -1-13出版社: 清华大学出版社打印第二本书的信息..书名: 年轻人与海作者: 海明威售价: 20.00出版日期: -1-13出版社: 北京大学出版社
现在用结构体指针
还是上面的例子,略作修改
#include <stdio.h>struct Date{int year;int month;int day;};struct Book{char title[128];char author[40];float price;struct Date date;char publisher[40];};void getInput(struct Book *book);void printBook(struct Book *book);void getInput(struct Book *book){printf("请输入书名: ");scanf("%s", book->title);printf("请输入作者: ");scanf("%s", book->author);printf("请输入售价: ");scanf("%f", &book->price);printf("请输入出版日期: ");scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);printf("请输入出版社: ");scanf("%s", book->publisher);};void printBook(struct Book *book){printf("书名: %s\n", book->title);printf("作者: %s\n", book->author);printf("售价: %.2f\n", book->price);printf("出版日期: %d-%d-%d\n", book->date.year,book->date.month,book->date.day);printf("出版社: %s\n", book->publisher);}int main(void){struct Book b1, b2;printf("请录入第一本书的信息...\n");getInput(&b1);putchar('\n');printf("请录入第二本书的信息...\n");getInput(&b2);printf("\n\n录入完毕,现在开始打印验证...\n\n");printf("打印第一本书的信息..\n");printBook(&b1);putchar('\n');printf("打印第二本书的信息..\n");printBook(&b2);return 0;}
输出结果
打印第一本书的信息..书名: 老人与海作者: 海明威售价: 20.00出版日期: -1-13出版社: 清华大学出版社打印第二本书的信息..书名: 年轻人与海作者: 海明威售价: 20.00出版日期: -1-13出版社: 北京大学出版社