Data structure in C / C ++

Struct in C / C ++ Arrays in C / C ++ allow you to define several types of variables that can hold the values ​​of several members of the same data type. But the structure is another type of data in the C / C ++ programming language, which allows you to combine other types of data.

Arrays in C / C ++ allow you to define several types of variables that can hold the values ​​of several members of the same data type. But the structure is another type of data in the C / C ++ programming language, which allows you to combine other types of data.

The structure is used to represent a record. Suppose you want to store the value of a book in your library. You can store the properties of the following book:

Title

Author

Theme

Book ID

Define a structure in C ++

To define structure, you must use struct statement. The struct statement defines a new data type, with more than one member in your program. The general form of the struct statement is as follows:

 struct [ ten cau truc ] { phan dinh nghia thanh vien ; phan dinh nghia thanh vien ; . phan dinh nghia thanh vien ; } [ mot hoac nhieu bien cau truc ]; 

Here, the option can be arbitrary and one member defines the variables as int i, float j or another variable definition . At the end of the structure definition, before the semicolon, You can define one or more structural variables (optional). Here's how to declare the Book structure variable:

 struct Books { char tieude [ 50 ]; char tacgia [ 50 ]; char chude [ 100 ]; int book_id ; } book ; 

Access members of the structure in C ++

To access any member of the structure, use the element access operator (.) . The encrypted member access operator is the dot between the structure variable name and the structure member you want to access. You will use struct keyword to define variables of structure type. Here is an example of how to use the structure in C ++:

 #include #include using namespace std ; struct Books { char tieude [ 50 ]; char tacgia [ 50 ]; char chude [ 100 ]; int book_id ; }; int main ( ) { struct Books QuyenSach1 ; // Declare QuyenSach1 of type Book struct Books QuyenSach2 ; // Declare QuyenSach2 of type Book // chi tiet ve quyen sach thu nhat strcpy ( QuyenSach1 . tieude , "Ngon ngu Lap trinh C++" ); strcpy ( QuyenSach1 . tacgia , "Pham Van At" ); strcpy ( QuyenSach1 . chude , "Lap trinh" ); QuyenSach1 . book_id = 1225 ; // chi tiet ve quyen sach thu hai strcpy ( QuyenSach2 . tieude , "Toi thay hoa vang tren co xanh" ); strcpy ( QuyenSach2 . tacgia , "Nguyen Nhat Anh" ); strcpy ( QuyenSach2 . chude , "Van hoc" ); QuyenSach2 . book_id = 3214 ; // in thong tin ve QuyenSach1 cout << "Tieu de cua Quyen sach thu nhat la: " << QuyenSach1 . tieude << endl ; cout << "Tac gia cua Quyen sach thu nhat la: " << QuyenSach1 . tacgia << endl ; cout << "Chu de cua Quyen sach thu nhat la: " << QuyenSach1 . chude << endl ; cout << "ID cua Quyen sach thu nhat la: " << QuyenSach1 . book_id << endl ; cout << "nn===================================================================nn" << endl ; // in thong tin ve QuyenSach2 cout << "Tieu de cua Quyen sach thu hai la: " << QuyenSach2 . tieude << endl ; cout << "Tac gia cua Quyen sach thu hai la: " << QuyenSach2 . tacgia << endl ; cout << "Chu de cua Quyen sach thu hai la: " << QuyenSach2 . chude << endl ; cout << "ID cua Quyen sach thu hai la: " << QuyenSach2 . book_id << endl ; return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Data structure in C / C ++ Picture 1Data structure in C / C ++ Picture 1

Structure as a function parameter in C ++

You can pass a structure as a function parameter in the same way as when you pass any other variable or pointer. You will access the structure variable in the same way you have access in the above example:

 #include #include using namespace std ; void inthongtin ( struct Books book ); struct Books { char tieude [ 50 ]; char tacgia [ 50 ]; char chude [ 100 ]; int book_id ; }; int main ( ) { struct Books QuyenSach1 ; // Khai bao QuyenSach1 la cua kieu Books struct Books QuyenSach2 ; // Khai bao QuyenSach2 la cua kieu Books // chi tiet ve quyen sach thu nhat strcpy ( QuyenSach1 . tieude , "Ngon ngu Lap trinh C++" ); strcpy ( QuyenSach1 . tacgia , "Pham Van At" ); strcpy ( QuyenSach1 . chude , "Lap trinh" ); QuyenSach1 . book_id = 1225 ; // chi tiet ve quyen sach thu hai strcpy ( QuyenSach2 . tieude , "Toi thay hoa vang tren co xanh" ); strcpy ( QuyenSach2 . tacgia , "Nguyen Nhat Anh" ); strcpy ( QuyenSach2 . chude , "Van hoc" ); QuyenSach2 . book_id = 3214 ; // in thong tin ve QuyenSach1 inthongtin ( QuyenSach1 ); // in thong tin ve QuyenSach2 inthongtin ( QuyenSach2 ); return 0 ; } void inthongtin ( struct Books book ) { cout << "Tieu de sach: " << book . tieude << endl ; cout << "Tac gia: " << book . tacgia << endl ; cout << "Chu de: " << book . chude << endl ; cout << "ID cua sach la: " << book . book_id << endl ; cout << "nn========================================nn" << endl ; } 

Compiling and running the above C ++ program will produce the following results:

Data structure in C / C ++ Picture 2Data structure in C / C ++ Picture 2

Cursor to structure in C ++

You can define pointer structure in the same way you define the pointer to any other variable as follows:

 struct Books * contro_struct ; 

You can now store the address of the structure variable in the pointer variable defined above. To find the address of a structure variable, set the operator & before the structure name as follows:

 contro_struct = & QuyenSach1 ; 

To access a member of a structure using pointers to that structure, you must use the -> operator as follows:

 contro_struct -> tieude ; 

Now we rewrite the example above using structured pointers, hoping this will be easy for you to understand this concept:

 #include #include using namespace std ; void inthongtin ( struct Books * book ); struct Books { char tieude [ 50 ]; char tacgia [ 50 ]; char chude [ 100 ]; int book_id ; }; int main ( ) { struct Books QuyenSach1 ; // Khai bao QuyenSach1 la cua kieu Books struct Books QuyenSach2 ; // Khai bao QuyenSach2 la cua kieu Book // thong tin chi thiet ve quyen sach thu nhat strcpy ( QuyenSach1 . tieude , "Ngon ngu Lap trinh C++" ); strcpy ( QuyenSach1 . tacgia , "Pham Van At" ); strcpy ( QuyenSach1 . chude , "Lap trinh" ); QuyenSach1 . book_id = 1225 ; // thong tin chi thiet ve quyen sach thu hai strcpy ( QuyenSach2 . tieude , "Toi thay hoa vang tren co xanh" ); strcpy ( QuyenSach2 . tacgia , "Nguyen Nhat Anh" ); strcpy ( QuyenSach2 . chude , "Van hoc" ); QuyenSach2 . book_id = 3214 ; // in thong tin cua QuyenSach1, bang cach truyen dia chi cua cau truc inthongtin ( & QuyenSach1 ); // in thong tin cua QuyenSach2, bang cach truyen dia chi cua cau truc inthongtin ( & QuyenSach2 ); return 0 ; } // Ham nay chap nhan con tro toi cau truc lam tham so. void inthongtin ( struct Books * book ) { cout << "Tieu de sach: " << book -> tieude << endl ; cout << "Tac gia: " << book -> tacgia << endl ; cout << "Chu de: " << book -> chude << endl ; cout << "ID cua sach: " << book -> book_id << endl ; cout << "nn========================================nn" << endl ; } 

Compiling and running the above C ++ program will produce the following results:

Data structure in C / C ++ Picture 3Data structure in C / C ++ Picture 3

Typedef keyword in C ++

There is an easier way to define structures or you can "alias" the types you create. For example:

 typedef struct { char tieude [ 50 ]; char tacgia [ 50 ]; char chude [ 100 ]; int book_id ; } Books ; 

Now, you can use Books directly to define the variables of the Books structure type without using the struct keyword. Here is an example:

 Books QuyenSach1 , QuyenSach2 ; 

You can use the typedef keyword in C ++ for non-structured forms, as follows:

 typedef long int * pint32 ; pint32 x , y , z ; 

For x, y and z are all pointers to long int.

According to Tutorialspoint

Previous article: Input / Output in C ++

Next lesson: Class (class) and Object in C ++

5 ★ | 1 Vote