May 11, 2021 C++
The C/C?array allows you to define variables that store data items of the same type, but the structure is another user-defined available data type in C?, which allows you to store different types of data items.
Structures are used to represent a record, and if you want to track the dynamics of books in your library, you might want to track the following properties for each book:
In order to define a structure, you must use a struct statement. The struct statement defines a new data type with multiple members, and the struct statement is formatted as follows:
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
Structure tag is optional, and each member definition is a standard variable definition, such as int i; O r float f; O r other valid variable definitions. A t the end of the structure definition, before the last sign, you can specify one or more structure variables, which is optional. Here's how to declare the Book structure:
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
}book;
In order to access members of the structure, we use the member access operator (.). A member access operator is a period between the name of the structure variable and the member of the structure that we want to access. Y ou can use the struct keyword to define variables for structure types. The following example demonstrates the use of structures:
#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; // 声明 Book1,类型为 Book
struct Books Book2; // 声明 Book2,类型为 Book
// Book1 详述
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// Book2 详述
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// 输出 Book1 信息
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;
// 输出 Book2 信息
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;
}
When the above code is compiled and executed, it produces the following results:
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
You can use structures as function parameters, passing parameters in a similar way to other types of variables or pointers. You can use the methods in the example above to access the structure variables:
#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; // 声明 Book1,类型为 Book
struct Books Book2; // 声明 Book2,类型为 Book
// Book1 详述
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// Book2 详述
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// 输出 Book1 信息
printBook( Book1 );
// 输出 Book2 信息
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;
}
When the above code is compiled and executed, it produces the following results:
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
You can define pointers to structures in a similar way to defining pointers to other type variables, as follows:
struct Books *struct_pointer;
You can now store the address of the structure variable in the pointer variable defined above. In order to find the address of the structure variable, place the operator in front of the structure name, as follows:
struct_pointer = &Book1;
In order to access the members of the structure with pointers to the structure, you must use the --gt; operator, as follows:
struct_pointer->title;
Let's use structure pointers to override the above examples, which will help you understand the concept of structural pointers:
struct_pointer->title;
Let's use structure pointers to override the above examples, which will help you understand the concept of structural pointers:
#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; // 声明 Book1,类型为 Book
struct Books Book2; // 声明 Book2,类型为 Book */
// Book1 详述
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// Book2 详述
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// 通过传 Book1 的地址来输出 Book1 信息
printBook( &Book1 );
// 通过传 Book2 的地址来输出 Book2 信息
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;
}
When the above code is compiled and executed, it produces the following results:
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
Here's a simpler way to define the structure, and you can give the type you created an "alias." For example:
typedef struct
{
char title[50];
char author[50];
char subject[100];
int book_id;
}Books;
You can now use Books directly to define variables of the Books type without using the struct keyword. Here are some examples:
Books Book1, Book2;
You can use the typedef keyword to define unstructructed types, as follows:
typedef long int *pint32; pint32 x, y, z;
x, y, and z are pointers to long int.