D語言結構體

D語言數組允許定義類型的變量,可容納同類但是結構體幾個數據項,它允許結合不同類型的數據項提供另一種用戶定義的數據類型。

結構用來表示一個記錄,假設要跟蹤圖書館中的書籍。可能希望跟蹤瞭解每本書的以下屬性:

  • 書名

  • 作者

  • 科目

  • 書籍編號ID

定義一個結構體

要定義一個結構,必須使用結構體struct語句。該結構語句定義了一個新的數據類型,項目不止一個成員。該結構語句的格式是這樣的:

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

結構體標籤是可選的,每個成員的定義是正常的變量定義,比如int i; or float f; 或任何其他有效的變量定義。在該結構的定義的結尾,最後的分號之前,可以指定一個或多個結構變量,但它是可選的。這裏是將聲明書的結構體:

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

訪問結構成員

要訪問一個結構的任何成員,我們使用成員訪問運算符(.)成員訪問運算編碼爲結構體變量名,而我們希望訪問結構成員之間的時間段。可以使用struct關鍵字來定義結構類型的變量。下面是例子來說明結構的用法:

import std.stdio; struct Books { char [] title; char [] author; char [] subject; int book_id; }; void main( ) { Books Book1; /* Declare Book1 of type Book */ Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ Book1.title = "D Programming".dup; Book1.author = "Raj".dup; Book1.subject = "D Programming Tutorial".dup; Book1.book_id = 6495407; /* book 2 specification */ Book2.title = "Lua Programming".dup; Book2.author = "Raj".dup; Book2.subject = "Lua Programming Tutorial".dup; Book2.book_id = 6495700; /* print Book1 info */ writeln( "Book 1 title : ", Book1.title); writeln( "Book 1 author : ", Book1.author); writeln( "Book 1 subject : ", Book1.subject); writeln( "Book 1 book_id : ", Book1.book_id); /* print Book2 info */ writeln( "Book 2 title : ", Book2.title); writeln( "Book 2 author : ", Book2.author); writeln( "Book 2 subject : ", Book2.subject); writeln( "Book 2 book_id : ", Book2.book_id); }

當上面的代碼被編譯並執行,它會產生以下結果:

Book 1 title : D Programming
Book 1 author : Raj
Book 1 subject : D Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Lua Programming
Book 2 author : Raj
Book 2 subject : Lua Programming Tutorial
Book 2 book_id : 6495700

結構作爲函數的參數

可以傳遞一個結構非常類似的方式函數作爲參數傳遞任何其他變量或指針。訪問在上面的例子已經使用類似的方式的結構變量:

import std.stdio; struct Books { char [] title; char [] author; char [] subject; int book_id; }; void main( ) { Books Book1; /* Declare Book1 of type Book */ Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ Book1.title = "D Programming".dup; Book1.author = "Raj".dup; Book1.subject = "D Programming Tutorial".dup; Book1.book_id = 6495407; /* book 2 specification */ Book2.title = "Lua Programming".dup; Book2.author = "Raj".dup; Book2.subject = "Lua Programming Tutorial".dup; Book2.book_id = 6495700; /* print Book1 info */ printBook( Book1 ); /* Print Book2 info */ printBook( Book2 ); } void printBook( Books book ) { writeln( "Book title : ", book.title); writeln( "Book author : ", book.author); writeln( "Book subject : ", book.subject); writeln( "Book book_id : ", book.book_id); }

當上面的代碼被編譯並執行,它會產生以下結果:

Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 6495407
Book title : Lua Programming
Book author : Raj
Book subject : Lua Programming Tutorial
Book book_id : 6495700

結構體初始化

結構可以有兩種形式,可使用構造函數和其他使用{}格式進行初始化。一個例子如下所示。

import std.stdio; struct Books { char [] title; char [] subject = "Empty".dup; int book_id = -1; char [] author = "Raj".dup; }; void main( ) { Books Book1 = Books("D Programming".dup, "D Programming Tutorial".dup, 6495407 ); printBook( Book1 ); Books Book2 = Books("Lua Programming".dup, "Lua Programming Tutorial".dup, 6495407,"Raj".dup ); printBook( Book2 ); Books Book3 = {title:"Obj C programming".dup, book_id : 1001}; printBook( Book3 ); } void printBook( Books book ) { writeln( "Book title : ", book.title); writeln( "Book author : ", book.author); writeln( "Book subject : ", book.subject); writeln( "Book book_id : ", book.book_id); }

當上面的代碼被編譯並執行,它會產生以下結果:

Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 6495407
Book title : Lua Programming
Book author : Raj
Book subject : Lua Programming Tutorial
Book book_id : 6495407
Book title : Obj C programming
Book author : Raj
Book subject : Empty
Book book_id : 1001

靜態成員

靜態變量初始化一次。例如有這本書唯一的ID,我們可以使book_id靜態和遞增書籍ID。一個例子如下所示。

import std.stdio; struct Books { char [] title; char [] subject = "Empty".dup; int book_id; char [] author = "Raj".dup; static int id = 1000; }; void main( ) { Books Book1 = Books("D Programming".dup, "D Programming Tutorial".dup,++Books.id ); printBook( Book1 ); Books Book2 = Books("Lua Programming".dup, "Lua Programming Tutorial".dup,++Books.id); printBook( Book2 ); Books Book3 = {title:"Obj C programming".dup, book_id:++Books.id}; printBook( Book3 ); } void printBook( Books book ) { writeln( "Book title : ", book.title); writeln( "Book author : ", book.author); writeln( "Book subject : ", book.subject); writeln( "Book book_id : ", book.book_id); }

當上面的代碼被編譯並執行,它會產生以下結果:

Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 1001
Book title : Lua Programming
Book author : Raj
Book subject : Lua Programming Tutorial
Book book_id : 1002
Book title : Obj C programming
Book author : Raj
Book subject : Empty
Book book_id : 1003