C/C++数组允许定义,结合同类型的多个数据项,但结构是另一个用户定义的数据类型,它允许结合不同种类的数据项变量。
结构是用来代表一个记录,假设想跟踪图书馆中的书籍。可能要跟踪有关每本书以下属性:
- Title(标题)
- Author(作者)
- Subject(科目)
- Book ID(编号)
定义一个结构:
定义一个结构,必须使用结构的语句。该结构语句定义了一个新的数据类型,具有多个成员,程序结构语句的格式应该是这样的:
struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];
结构变量是可选的,每个成员的定义是一个正常的变量定义,如int i; or float f; 或任何其他有效的变量的定义。在该结构的定义的结尾,最后的分号之前,可以指定一个或多个结构变量,但它是可选的。这里是声明图书的结构方式:
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_yiibaier;
现在,可以存储结构变量的地址在上面定义的指针变量。为了找到一个结构变量的地址,&操作符结构的名字前,如下所示:
struct_yiibaier = &Book1;
使用一个指向结构访问结构的成员,必须使用- >操作如下:
struct_yiibaier->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 yiibaier 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 关键字
有一个简单的方法来定义结构,或者可以使用“alias”类型来创建。例如:
typedef struct { char title[50]; char author[50]; char subject[100]; int book_id; }Books;
现在,可以使用上面书籍,无需使用struct关键字来定义图书类型变量。下面是一个例子:
Books Book1, Book2;
可以使用typedef关键字用于非结构以及如下:
typedef long int *pint32; pint32 x, y, z;
x,y和z是所有的指针到长整型