結(jié)構(gòu)Structs用于表示記錄,假設(shè)您想跟蹤圖書館中的書籍,您可能需要跟蹤有關(guān)每本書的以下屬性-
要定義結(jié)構(gòu),必須使用 struct 語句, struct語句定義了一種新的數(shù)據(jù)類型, struct語句的格式如下:
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
結(jié)構(gòu)標簽tag是可選的,每個成員定義都是一個普通的變量定義,如int i;或浮動f,在分號前面的結(jié)構(gòu)定義末尾,您可以指定一個或多個可選的結(jié)構(gòu)變量。這是聲明Books結(jié)構(gòu)的方式-
struct Books {
char [] title;
char [] author;
char [] subject;
int book_id;
};
要訪問結(jié)構(gòu)的任何成員,請使用成員訪問運算符(.),成員訪問運算符被編碼為結(jié)構(gòu)變量名和我們希望訪問的結(jié)構(gòu)成員之間的句號,您可以使用 struct 關(guān)鍵字定義結(jié)構(gòu)類型的變量。
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="D Programming".dup;
Book2.author="Raj".dup;
Book2.subject="D 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);
}
編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-
Book 1 title : D Programming
Book 1 author : Raj
Book 1 subject : D Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : D Programming
Book 2 author : Raj
Book 2 subject : D Programming Tutorial
Book 2 book_id : 6495700
您可以通過與傳遞任何其他變量或指針非常相似的方式將結(jié)構(gòu)作為函數(shù)參數(shù)傳遞,您將以與上述示例相同的方式訪問結(jié)構(gòu)變量-
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="D Programming".dup;
Book2.author="Raj".dup;
Book2.subject="D 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);
}
編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-
Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 6495407
Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 6495700
可以用兩種形式初始化結(jié)構(gòu),一種使用構(gòu)造函數(shù),另一種使用{}格式。一個如下所示。
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("D Programming".dup,
"D 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);
}
編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-
Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 6495407
Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 6495407
Book title : Obj C programming
Book author : Raj
Book subject : Empty
Book book_id : 1001
靜態(tài)變量僅初始化一次,如要獲得書籍的唯一ID,我們可以將book_id設(shè)為static靜態(tài)類型 ,并增加書籍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("D Programming".dup, "D 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);
}
編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-
Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 1001
Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 1002
Book title : Obj C programming
Book author : Raj
Book subject : Empty
Book book_id : 1003
更多建議: