C++ 结构体 (struct)
C++ 结构体是用户定义的数据类型,用于将不同类型的相关变量组合到一个名称下。结构体也称为 structs。
结构体用于表示一条记录,假设您想跟踪图书馆中的图书。您可能需要跟踪每本书的以下属性 -
- Title
- Author
- Subject
- Book ID
定义结构体
要定义结构体,必须使用 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关键字来定义结构体类型的变量。
示例
以下示例解释了结构的用法 -
#include <iostream> #include <cstring> using namespace std; struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main() { struct Books Book1; // 将 Book1 声明为 Book 类型 struct Books Book2; // 将 Book2 声明为 Book 类型 // 书籍 1 的规范 strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "Chand Miyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; // 书籍 2 的规范 strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Yakit Singha"); strcpy( Book2.subject, "Telecom"); Book2.book_id = 6495700; // 打印 Book1 信息 cout << "Book 1 title : " << Book1.title <<endl; cout << "Book 1 author : " << Book1.author <<endl; cout << "Book 1 subject : " << Book1.subject <<endl; cout << "Book 1 id : " << Book1.book_id <<endl; // 打印 Book2 信息 cout << "Book 2 title : " << Book2.title <<endl; cout << "Book 2 author : " << Book2.author <<endl; cout << "Book 2 subject : " << Book2.subject <<endl; cout << "Book 2 id : " << Book2.book_id <<endl; return 0; }
当编译并执行上述代码时,它会产生以下结果 -
Book 1 title : Learn C++ Programming Book 1 author : Chand Miyan Book 1 subject : C++ Programming Book 1 id : 6495407 Book 2 title : Telecom Billing Book 2 author : Yakit Singha Book 2 subject : Telecom Book 2 id : 6495700
结构体作为函数参数
您可以将结构体作为函数参数传递,其方式与传递其他变量或指针非常相似。您可以像访问上述示例中的类似方式访问结构体变量 -
示例
#include <iostream> #include <cstring> using namespace std; void printBook( struct Books book ); struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main() { struct Books Book1; // 将 Book1 声明为 Book 类型 struct Books Book2; // 将 Book2 声明为 Book 类型 // 书籍 1 的规范 strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "Chand Miyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; // 书籍 2 的规范 strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Yakit Singha"); strcpy( Book2.subject, "Telecom"); Book2.book_id = 6495700; // 打印 Book1 信息 printBook( Book1 ); // 打印 Book2 信息 printBook( Book2 ); return 0; } void printBook( struct Books book ) { cout << "Book title : " << book.title <<endl; cout << "Book author : " << book.author <<endl; cout << "Book subject : " << book.subject <<endl; cout << "Book id : " << book.book_id <<endl; }
当编译并执行上述代码时,它会产生以下结果 -
Book title : Learn C++ Programming Book author : Chand Miyan Book subject : C++ Programming Book id : 6495407 Book title : Telecom Billing Book author : Yakit Singha Book subject : Telecom Book id : 6495700
指向结构的指针
您可以像定义指向任何其他变量的指针一样定义指向结构的指针,如下所示 -
语法
struct Books *struct_pointer;
现在,您可以将结构变量的地址存储在上面定义的指针变量中。要查找结构变量的地址,请在结构名称前放置 & 运算符,如下所示 -
语法
struct_pointer = &Book1;
要使用指向结构的指针访问结构的成员,必须使用 ->运算符如下 -
语法
struct_pointer->title;
示例
让我们使用结构体指针重写上面的例子,希望这能帮助您轻松理解这个概念 -
#include <iostream> #include <cstring> using namespace std; void printBook( struct Books *book ); struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main() { struct Books Book1; // 将 Book1 声明为 Book 类型 struct Books Book2; // 将 Book2 声明为 Book 类型 // Book 1 的说明 strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "Chand Miyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; // Book 2 的说明 strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Yakit Singha"); strcpy( Book2.subject, "Telecom"); Book2.book_id = 6495700; // 打印 Book1 的信息,传递结构体的地址 printBook( &Book1 ); // 打印 Book1 的信息,传递结构体的地址 printBook( &Book2 ); return 0; } // 此函数接受指向结构的指针作为参数。 void printBook( struct Books *book ) { cout << "Book title : " << book->title <<endl; cout << "Book author : " << book->author <<endl; cout << "Book subject : " << book->subject <<endl; cout << "Book id : " << book->book_id <<endl; }
当编译并执行上述代码时,它会产生以下结果 -
Book title : Learn C++ Programming Book author : Chand Miyan Book subject : C++ Programming Book id : 6495407 Book title : Telecom Billing Book author : Yakit Singha Book subject : Telecom Book id : 6495700
typedef 关键字
有一种更简单的方法来定义结构体,或者您可以为您创建的类型添加"别名"。
示例
typedef struct { char title[50]; char author[50]; char subject[100]; int book_id; } Books;
现在,您可以直接使用 Books 来定义 Books 类型的变量,而无需使用 struct 关键字。以下是示例 -
Books Book1, Book2;
除了以下用法外,您还可以对非结构体使用 typedef 关键字:-
typedef long int *pint32; pint32 x, y, z;
x、y 和 z 都是指向长整型的指针。