结构体
结构体可以用于存储不同类型的数据,还可以保持成员(Menber)信息的独立
1. 结构体的声明和结构体变量的定义
#include <stdio.h> //标准输入输出头文件
#include <string.h> //字符串处理函数的头文件
int main() {
struct Person {
char name[20];
char address[20];
} A; //结构体模板声明:struct 结构体类型名 {成员变量列表......} 结构体变量名; 其中Person是结构体类型名,name和address是成员变量,A是结构体变量名
struct Person B; //声明好结构体模板后,就可以通过 struct 结构体类型名 结构体变量名 来声明结构体变量;
strcpy_s(A.name, "Li Ming");
printf("%s", A.name);
strcpy_s(B.name, "Li Hua");
printf("%s", B.name);
return 0;
}
2.结构体数组
#include <stdio.h>
#include <string.h>
#define MAXTITL 30
#define MAXAUTH 30
#define MAXBKS 100
struct book { //声明结构体模板
char title[MAXTITL];
char author[MAXAUTH];
float value;
};
struct book library[MAXBKS]; //声明结构体数组,其中library[0],library[1],library[2]...为相互独立的结构体变量
int main() {
strcpy_s(library[0].title, "A tale of two cities"); // library[0].title 用于调用结构体变量library[0]的成员变量title
strcpy_s(library[0].author, "Dickens");
library[0].value = 30;
printf("%s\n%s\n%f\n", library[0].title, library[0].author, library[0].value);
}
3.嵌套结构
#include <stdio.h>
#include <string.h>
#include <math.h>
struct point {
float x;
float y;
};
struct rect {
struct point pt1;
struct point pt2;
};
struct rect A;
int main() {
printf("请输入长方形的左下顶点坐标\n");
scanf_s("%f,%f", &A.pt1.x, &A.pt1.y);
printf("请输入长方形的右上顶点坐标\n");
scanf_s("%f,%f", &A.pt2.x, &A.pt2.y);
printf("长方形的面积为:%f\n",(A.pt2.x- A.pt1.x)*(A.pt2.y- A.pt1.y));
}
4.指向结构的指针
#include <stdio.h>
#include <string.h>
#define LEN 20
struct name {
char first[LEN];
char last[LEN];
};
struct guy {
struct name handle;
char favfood[LEN];
char job[LEN];
float income;
};
int main(void)
{
struct guy fellow[2] = {
{{"Ewen","Villard"},"grilled salmon","personality coach",68112.00},
{{"Rodney","Swillbelly"}, "tripe", "tabloid editor", 432400.00}
};
struct guy* him;
printf("address #1:%p #2: %p\n", &fellow[0], &fellow[1]);
him = fellow;
printf("address #1:%p #2: %p\n", him, him+1);
printf("him->handle.first %s,him->handle.last is %s\n", him->handle.first, him->handle.last);
printf("him->income is $%.2f,(*him)->income is $%.2f\n", him->income,(*him).income);
};
5.结构体作为函数参数
#include <stdio.h>
#include <string.h>
/*声明结构体模板*/
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* 函数声明 */
void printBook(struct Books book);
int main()
{
struct Books Book1; /* 声明 Book1,类型为 Books */
struct Books Book2; /* 声明 Book2,类型为 Books */
/* Book1 详述 */
strcpy_s(Book1.title, "C Programming");
strcpy_s(Book1.author, "Nuha Ali");
strcpy_s(Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* Book2 详述 */
strcpy_s(Book2.title, "Telecom Billing");
strcpy_s(Book2.author, "Zara Ali");
strcpy_s(Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* 输出 Book1 信息 */
printBook(Book1);
/* 输出 Book2 信息 */
printBook(Book2);
return 0;
}
/*定义函数*/
void printBook(struct Books book)
{
printf("Book title : %s\n", book.title);
printf("Book author : %s\n", book.author);
printf("Book subject : %s\n", book.subject);
printf("Book book_id : %d\n", book.book_id);
}