C++ Data Structures
C/C++ arrays allow you to define variables that can store items of the same type, but structures are another user-defined data type available in C++ that allows you to store items of different types.
Structures are used to represent a record. Suppose you want to track the movements of books in a library. You might need to track the following attributes for each book:
- Title: Title
- Author: Author
- Subject: Category
- Book ID: Book ID
Defining a Structure
To define a 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;
typename is the name of the structure type, and membertype1 member_name1 is the standard variable definition, such as int i; or float f;, or any other valid variable definition. At the end of the structure definition, before the final semicolon, you can specify one or more structure variables, which is optional. Below is the declaration of a structure type Books, with variable book:
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Accessing Structure Members
To access members of a structure, we use the member access operator (.). The member access operator is a period between the structure variable name and the structure member we want to access.
The following example demonstrates the use of structures:
Example
#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; // Define a variable of structure type Books
Books Book2; // Define another variable of structure type Books
// Details of Book1
strcpy( Book1.title, "C++ Tutorial");
strcpy( Book1.author, "tutorialpro");
strcpy( Book1.subject, "Programming Language");
Book1.book_id = 12345;
// Details of Book2
strcpy( Book2.title, "CSS Tutorial");
strcpy( Book2.author, "tutorialpro");
strcpy( Book2.subject, "Frontend Technology");
Book2.book_id = 12346;
// Output Book1 information
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;
// Output Book2 information
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;
}
The example defines the structure type Books and its two variables Book1 and Book2. When the above code is compiled and executed, it produces the following result:
Book 1 Title : C++ Tutorial
Book 1 Author : tutorialpro
Book 1 Subject : Programming Language
Book 1 ID : 12345
Book 2 Title : CSS Tutorial
Book 2 Author : tutorialpro
Book 2 Subject : Frontend Technology
Book 2 ID : 12346
Structures as Function Arguments
You can pass a structure as a function argument in the same way as you pass any other variable or pointer. You can pass structures as function parameters in a similar way to other types of variables or pointers. You can access structure variables using the method shown in the example above:
Example
#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; // Define a variable of type Books
Books Book2; // Define another variable of type Books
// Details of Book1
strcpy(Book1.title, "C++ Tutorial");
strcpy(Book1.author, "tutorialpro");
strcpy(Book1.subject, "Programming Language");
Book1.book_id = 12345;
// Details of Book2
strcpy(Book2.title, "CSS Tutorial");
strcpy(Book2.author, "tutorialpro");
strcpy(Book2.subject, "Front-end Technology");
Book2.book_id = 12346;
// Print Book1 information
printBook(Book1);
// Print Book2 information
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 result:
Book Title : C++ Tutorial
Book Author : tutorialpro
Book Subject : Programming Language
Book ID : 12345
Book Title : CSS Tutorial
Book Author : tutorialpro
Book Subject : Front-end Technology
Book ID : 12346
Pointer to Structure
You can define pointers to structures in the same way as you define pointers to other types of variables, as shown below:
struct Books *struct_pointer;
Now, you can store the address of a structure variable in the pointer variable defined above. To find the address of a structure variable, place the & operator before the structure name, as shown below:
struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must use the -> operator, as shown below:
struct_pointer->title;
Let's rewrite the above example using a pointer to structure, which will help you understand the concept of structure pointers:
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; // Define a variable of type Books
Books Book2; // Define another variable of type Books
// Details of Book1
strcpy(Book1.title, "C++ Tutorial");
strcpy(Book1.author, "tutorialpro");
strcpy(Book1.subject, "Programming Language");
Book1.book_id = 12345;
// Details of Book2
strcpy(Book2.title, "CSS Tutorial");
strcpy(Book2.author, "tutorialpro");
strcpy(Book2.subject, "Front-end Technology");
Book2.book_id = 12346;
// Print Book1 information
printBook(&Book1);
// Print Book2 information
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;
}
// Output Book1 information by passing the address of Book1 printBook(&Book1);
// Output Book2 information by passing the address of Book2 printBook(&Book2);
return 0; } // This function takes a structure pointer as a 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; }
When the above code is compiled and executed, it produces the following result:
Book Title : C++ Tutorial
Book Author : tutorialpro
Book Subject : Programming Language
Book ID : 12345
Book Title : CSS Tutorial
Book Author : tutorialpro
Book Subject : Frontend Technology
Book ID : 12346
typedef Keyword
Below is a simpler way to define a structure, where you can create an "alias" for the created type. For example:
typedef struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} Books;
Now, you can directly use Books to define variables of type Books without needing to use the struct keyword. Here is an example:
Books Book1, Book2;
You can also use the typedef keyword to define non-structure types, as shown below:
typedef long int *pint32;
pint32 x, y, z;
x, y, and z are all pointers to long int.