C 语言结构体详解:高效组织复杂数据
数组允许定义可容纳多个同类数据项的变量类型。类似地,结构体 是 C 语言另一种用户定义数据类型,允许组合不同种类的数据项。
结构体用于表示记录。假设要跟踪图书馆图书,可能需记录每本书的以下属性:
- 标题
- 作者
- 主题
- 书号
定义结构体
定义结构体须使用 struct 语句。struct 语句定义新数据类型,包含多个成员。struct 语句格式:
struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];
结构体标记 可选,每个成员定义为普通变量定义,如 int i; 或 float f; 等。结构体定义结束、最后分号前,可指定一个或多个结构体变量(可选)。声明 Book 结构体方式:
struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;
访问结构体成员
访问结构体任意成员使用成员访问运算符 (.)。成员访问运算符为结构体变量名与成员名之间的句点。使用关键字 struct 定义结构体类型变量。下例展示程序中使用结构体:
Live Demo
#include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { struct Books Book1; /* Declare Book1 of type Book */ struct Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* book 2 specification */ strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali"); strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* print Book1 info */ printf( "Book 1 title : %s\n", Book1.title); printf( "Book 1 author : %s\n", Book1.author); printf( "Book 1 subject : %s\n", Book1.subject); printf( "Book 1 book_id : %d\n", Book1.book_id); /* print Book2 info */ printf( "Book 2 title : %s\n", Book2.title); printf( "Book 2 author : %s\n", Book2.author); printf( "Book 2 subject : %s\n", Book2.subject); printf( "Book 2 book_id : %d\n", Book2.book_id); return 0; }
编译执行输出:
Book 1 title : C Programming Book 1 author : Nuha Ali Book 1 subject : C Programming Tutorial Book 1 book_id : 6495407 Book 2 title : Telecom Billing Book 2 author : Zara Ali Book 2 subject : Telecom Billing Tutorial Book 2 book_id : 6495700
结构体作为函数参数
可像传递其他变量或指针一样传递结构体作为函数参数。
Live Demo
#include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; /* function declaration */ void printBook( struct Books book ); int main( ) { struct Books Book1; struct Books Book2; strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali"); strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; printBook( Book1 ); 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); }
编译执行输出:
Book title : C Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700
结构体指针
可像定义其他变量指针一样定义结构体指针:
struct Books *struct_pointer;
现在可将结构体变量地址存入上述指针。获取结构体变量地址,在结构体名前加 '&' 运算符:
struct_pointer = &Book1;
通过指针访问结构体成员必须使用 -> 运算符:
struct_pointer->title;
用结构体指针重写上例:
Live Demo
#include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; /* function declaration */ void printBook( struct Books *book ); int main( ) { struct Books Book1; struct Books Book2; strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali"); strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; printBook( &Book1 ); 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); }
编译执行输出:
Book title : C Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700
位域
位域允许在结构体中紧凑打包数据。当内存或数据存储紧张时特别有用。典型例子:
- 将多个对象打包进一个机器字。如 1 位标志可压缩。
- 读取外部文件格式——非标准格式可读入,如 9 位整数。
C 允许在结构体定义中在变量后加 :bit length 实现。例如:
struct packed_struct { unsigned int f1:1; unsigned int f2:1; unsigned int f3:1; unsigned int f4:1; unsigned int type:4; unsigned int my_int:9; } pack;
packed_struct 包含 6 个成员:四个 1 位标志 f1..f3、4 位 type 和 9 位 my_int。
C 自动将位域尽可能紧凑打包,前提是最大字段长度不超过计算机整数字长。否则某些编译器可能允许字段内存重叠,或将下一字段存入下一字。
C语言
- 革新数据存储:带隐形斗篷的次世代磁性纳米结构
- 2021程序员必读17本经典编程书单:从代码技艺到架构思维的进阶之路
- 3D打印晶格结构赋能零件性能:轻量化、强化与减震
- C++结构体与类:嵌入式开发者实战指南
- Nucleus RTOS信号量:实用服务与数据结构
- 开创液态环境下电子束3D打印微凝胶结构
- 书评:《数字化或死亡——IoT驱动转型蓝图》
- 颠覆性技术时代如何留住人性
- C 语言 Typedef 详解:自定义类型名简化代码
- C++数据结构实战:结构体struct定义、访问、函数参数、指针与typedef别名
- C# 结构体完全指南:定义、特性、与类的区别及实战
- Java 经典数据结构:Vector、Stack、Hashtable 等遗留类详解