結(jié)構(gòu)體

2018-08-12 22:03 更新

結(jié)構(gòu)體

C/C++ 結(jié)構(gòu)體(struct)是由一系列具有相同類型或不同類型的數(shù)據(jù)構(gòu)成的數(shù)據(jù)集合,叫做結(jié)構(gòu),但結(jié)構(gòu)體(structure)是一種用戶定義的數(shù)據(jù)類型,允許你將不同類型的數(shù)據(jù)項(xiàng)放在一起。

結(jié)構(gòu)用來(lái)表示一條記錄。假設(shè)你想在圖書館中找一本書,您可能需要查找每本書的以下屬性:

  • Title
  • Author
  • Subject
  • Book ID

定義一個(gè)結(jié)構(gòu)體

定義一個(gè)結(jié)構(gòu)體,您必須使用結(jié)構(gòu)體聲明。結(jié)構(gòu)體語(yǔ)句為您的程序定義了一個(gè)新的數(shù)據(jù)類型,擁有一個(gè)以上的成員。

結(jié)構(gòu)體聲明的格式是這樣的:

    struct [structure tag]
    {
       member definition;
       member definition;
       ...
       member definition;
    } [one or more structure variables];  

structure tag 是可選的,每個(gè)成員的定義都是一個(gè)正常的變量定義,如 int i;或者 float f;或者任何其他有效的變量定義。在結(jié)構(gòu)的定義結(jié)束時(shí),在結(jié)構(gòu)體定義結(jié)尾處的(“;”)符號(hào)之前可以指定一個(gè)或多個(gè)結(jié)構(gòu)變量,但它是可選的。這是聲明書結(jié)構(gòu)體的方式:

    struct Books
    {
       char  title[50];
       char  author[50];
       char  subject[100];
       int   book_id;
    }book;  

訪問(wèn)結(jié)構(gòu)體成員

訪問(wèn)一個(gè)結(jié)構(gòu)體的任何成員,我們使用 member access operator(成員訪問(wèn)操作符):(.) 來(lái)訪問(wèn)結(jié)構(gòu)體成員。成員訪問(wèn)操作符編碼為結(jié)構(gòu)變量名和我們要訪問(wèn)結(jié)構(gòu)成員之間的一個(gè)點(diǎn)符號(hào)。使用關(guān)鍵字 struct 來(lái)定義結(jié)構(gòu)類型的變量。下面是例子解釋怎樣使用結(jié)構(gòu)體:

    #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;
    }

編譯和執(zhí)行上面的代碼,執(zhí)行結(jié)果如下:

    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

結(jié)構(gòu)體作為函數(shù)參數(shù)

你可以將結(jié)構(gòu)體作為函數(shù)參數(shù)傳遞,其使用方式和將其他任何變量或指針作為參數(shù)傳遞非常相似。你可以以同樣的方式訪問(wèn)結(jié)構(gòu)變量,就如在上面的例子中顯示的一樣:

    #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;
    }

編譯和執(zhí)行上面的代碼,執(zhí)行結(jié)果如下:

    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

結(jié)構(gòu)體指針

您可以定義結(jié)構(gòu)體指針,以一種定義指向其他變量的指針非常相似的方式,如下所示:

    struct Books *struct_pointer;

現(xiàn)在,您可以用上面定義的指針變量存儲(chǔ)一個(gè)結(jié)構(gòu)變量的地址。找到一個(gè)結(jié)構(gòu)變量的地址,把操作符 & 置于結(jié)構(gòu)體名稱的前面,如下所示:

    struct_pointer = &Book1;

為了通過(guò)一個(gè)指向結(jié)構(gòu)的指針訪問(wèn)結(jié)構(gòu)體成員,必須使用 -> 操作符,如下所示:

    struct_pointer->title;

讓我們使用結(jié)構(gòu)指針重寫上面的例子,希望這將幫助你更容易理解這個(gè)概念:

    #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;
    }

編譯和執(zhí)行上面的代碼,執(zhí)行結(jié)果如下:

    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 關(guān)鍵字

有一個(gè)更簡(jiǎn)單的方法來(lái)定義結(jié)構(gòu)體,你可以給你創(chuàng)建的類型起一個(gè)別名,例如:

    typedef struct
    {
       char  title[50];
       char  author[50];
       char  subject[100];
       int   book_id;
    }Books;

現(xiàn)在,可以直接使用 Books 來(lái)定義書籍類型的變量,而不使用 struct 關(guān)鍵字。下面是示例:

    Books Book1, Book2;

你也可以在非結(jié)構(gòu)體(non-structs)中使用 typedef 關(guān)鍵字,如下所示:

    typedef long int *pint32;

    pint32 x, y, z;

x,y,z 是指向長(zhǎng)整數(shù)類型的指針。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)