
- C++ 基礎
- C++ 首頁
- C++ 概述
- C++ 環境設定
- C++ 基本語法
- C++ 註釋
- C++ Hello World
- C++ 省略名稱空間
- C++ 常量/字面量
- C++ 關鍵字
- C++ 識別符號
- C++ 資料型別
- C++ 數值資料型別
- C++ 字元資料型別
- C++ 布林資料型別
- C++ 變數型別
- C++ 變數作用域
- C++ 多個變數
- C++ 基本輸入/輸出
- C++ 修飾符型別
- C++ 儲存類
- C++ 運算子
- C++ 數字
- C++ 列舉
- C++ 引用
- C++ 日期和時間
- C++ 控制語句
- C++ 決策
- C++ if 語句
- C++ if else 語句
- C++ 巢狀 if 語句
- C++ switch 語句
- C++ 巢狀 switch 語句
- C++ 迴圈型別
- C++ while 迴圈
- C++ for 迴圈
- C++ do while 迴圈
- C++ foreach 迴圈
- C++ 巢狀迴圈
- C++ break 語句
- C++ continue 語句
- C++ goto 語句
- C++ 建構函式
- C++ 建構函式和解構函式
- C++ 複製建構函式
C++ 結構體 (struct)
C++ 結構體是使用者自定義的資料型別,用於將不同型別的相關變數組合在一個名稱下。結構體也稱為結構。
結構體用於表示記錄,例如,您想跟蹤圖書館中圖書的資訊。您可能需要跟蹤每本書的以下屬性:
- 標題
- 作者
- 主題
- 圖書 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; // Declare Book1 of type Book struct Books Book2; // Declare Book2 of type Book // book 1 specification strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "Chand Miyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; // book 2 specification strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Yakit Singha"); strcpy( Book2.subject, "Telecom"); Book2.book_id = 6495700; // Print Book1 info 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; // Print Book2 info 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; // Declare Book1 of type Book struct Books Book2; // Declare Book2 of type Book // book 1 specification strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "Chand Miyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; // book 2 specification strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Yakit Singha"); strcpy( Book2.subject, "Telecom"); Book2.book_id = 6495700; // Print Book1 info printBook( Book1 ); // Print Book2 info 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; // Declare Book1 of type Book struct Books Book2; // Declare Book2 of type Book // Book 1 specification strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "Chand Miyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; // Book 2 specification strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Yakit Singha"); strcpy( Book2.subject, "Telecom"); Book2.book_id = 6495700; // Print Book1 info, passing address of structure printBook( &Book1 ); // Print Book1 info, passing address of structure printBook( &Book2 ); return 0; } // This function accept pointer to structure as parameter. 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 都是指向長整型的指標。