C/C++ arrays allow you to define variables that can store the same type of data items, but the structure is another user-defined available data type in C++, which allows you to store different types of data items.
Structure is used to represent a record. Assuming you want to track the dynamics of books in the library, you may need to track the following properties of each book:
- Title: Title
- Author: Author
- Subject: Category
- Book ID: Book ID
Defining structure
To define the structure, you must use the struct statement. The struct statement defines a new data type with multiple members. The format of the struct statement is as follows:
struct type_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names;
type_name is the name of the structure type, member_type 1 member_name 1 is the standard variable definition, such as int i; or float f; or other valid variable definition. At the end of the structure definition, before the last semicolon, you can specify one or more structural variables, which is optional. Following is a declaration of a structure type Books with the variable book:
struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;
Access Structure Members
To access members of a structure, we use the member access operator (.). The member access operator is a period between the name of the structural variable and the structural member we want to access.
The following example demonstrates the usage of the structure:
#include <iostream> #include <cstring> using namespace std; // Declare a structure type Books struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { Books Book1; // Variable Book1 Defining Structural Type Books Books Book2; // Variable Book2 Defining Structural Type Books // Book1 details strcpy( Book1.title, "C++ Course"); strcpy( Book1.author, "Runoob"); strcpy( Book1.subject, "Programing language"); Book1.book_id = 12345; // Book2 details strcpy( Book2.title, "CSS Course"); strcpy( Book2.author, "Runoob"); strcpy( Book2.subject, "front-end technology"); Book2.book_id = 12346; // Output Book1 information cout << "Title of the first book : " << Book1.title <<endl; cout << "First Book Author : " << Book1.author <<endl; cout << "First Book Category : " << Book1.subject <<endl; cout << "First book ID : " << Book1.book_id <<endl; // Output Book2 information cout << "Title of the second book : " << Book2.title <<endl; cout << "Second Book Author : " << Book2.author <<endl; cout << "Second Category of Books : " << Book2.subject <<endl; cout << "Second books ID : " << Book2.book_id <<endl; return 0; }
A structure similar to Books and its two variables Book1 and Book2 are defined in the example. When the above code is compiled and executed, it produces the following results:
Title of the first book: C++ tutorial First book author: Runoob First Book Category: Programming Languages First book ID: 12345 Second book title: CSS tutorial Author of the second book: Runoob Second Book Category: Front-end Technology Book II ID: 12346
Structure as a function parameter
You can take the structure as a function parameter, passing parameters in a way similar to other types of variables or pointers. You can access structural variables in the way shown in the example above:
#include <iostream> #include <cstring> using namespace std; void printBook( struct Books book ); // Declare a structure type Books struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { Books Book1; // Variable Book1 Defining Structural Type Books Books Book2; // Variable Book2 Defining Structural Type Books // Book1 details strcpy( Book1.title, "C++ Course"); strcpy( Book1.author, "Runoob"); strcpy( Book1.subject, "Programing language"); Book1.book_id = 12345; // Book2 details strcpy( Book2.title, "CSS Course"); strcpy( Book2.author, "Runoob"); strcpy( Book2.subject, "front-end technology"); Book2.book_id = 12346; // Output Book1 information printBook( Book1 ); // Output Book2 information printBook( Book2 ); return 0; } void printBook( struct Books book ) { cout << "Title of book : " << book.title <<endl; cout << "Author of book : " << book.author <<endl; cout << "Book category : " << book.subject <<endl; cout << "book ID : " << book.book_id <<endl; }
When the above code is compiled and executed, it produces the following results:
Title: C++ Course Author: Runoob Bibliography: Programming Language Book ID: 12345 Title: CSS tutorial Author: Runoob Bibliography: Front-end Technology Book ID: 12346
Pointer to structure
You can define pointers to structures in a similar way as pointers to other types of variables, as follows:
struct Books *struct_pointer;
Now you can store the address of the structure variable in the pointer variable defined above. To find the address of the structure variable, put the & operator before the structure name, as follows:
struct_pointer = &Book1;
In order to access members of a structure using pointers pointing to that structure, you must use the - > operator, as follows:
struct_pointer->title;
Let's rewrite the above example using a structure pointer, which will help you understand the concept of a structure pointer:
Example
#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( ) { Books Book1; // Variable Book1 Defining Structural Type Books Books Book2; // Variable Book2 Defining Structural Type Books // Book1 details strcpy( Book1.title, "C++ Course"); strcpy( Book1.author, "Runoob"); strcpy( Book1.subject, "Programing language"); Book1.book_id = 12345; // Book2 details strcpy( Book2.title, "CSS Course"); strcpy( Book2.author, "Runoob"); strcpy( Book2.subject, "front-end technology"); Book2.book_id = 12346; // Output Book1 information by passing the address of Book1 printBook( &Book1 ); // Output Book2 information by passing the address of Book2 printBook( &Book2 ); return 0; } // The function takes the structure pointer as its parameter void printBook( struct Books *book ) { cout << "Title of book : " << book->title <<endl; cout << "Author of book : " << book->author <<endl; cout << "Book category : " << book->subject <<endl; cout << "book ID : " << book->book_id <<endl; }
When the above code is compiled and executed, it produces the following results:
Title: C++ Course Author: Runoob Bibliography: Programming Language Book ID: 12345 Title: CSS tutorial Author: Runoob Bibliography: Front-end Technology Book ID: 12346
typedef keyword
Here's a simpler way to define the structure. You can take an "alias" for the type you create. For example:
typedef struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }Books;
Now you can use Books directly to define variables of Books type without using the struct keyword. The following are examples:
Books Book1, Book2;
You can use the typedef keyword to define unstructured types, as follows:
typedef long int *pint32; pint32 x, y, z;
x, y and z are pointers to long int egers.