Chapter Three: Introduction to Data Structures in C++
C++ allows you to use different variables and structures, such as arrays and lists. We have looked at these in brief in the first book. This chapter introduces the different ways you can use these data structures to perform different activities in C++. You can use arrays to define different variables or combine different elements across the program or code into one variable, as long as they fall into the same category. A structure, however, allows you to combine different variables and data types. You can use a structure to define or represent records. Let us assume you want to track the books on your bookshelf. You can use a structure to track various attributes of every book on your shelf, such as:
-
Book ID
-
Book title
-
Genre
-
Author
The Struct Statement
You need to use the struct statement to define a structure in your code. This statement allows you to develop or define a new data type for your code. You can also define the number of elements or members in the code. The syntax of this statement is as follows:
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
It is not mandatory to use the structure tag when you use the statement. When you define a member in the structure, you can use the variable definition method we discussed in the previous book. For instance, you can use the method int i to define an integer
variable. The section before the semicolon in the struct syntax is also optional, but this is where you define the structure variables you want to use. Continuing with the example above, let us look at how you can define a book structure.
struct Books {
int book_id;
char book_title[50];
char genre[50];
char author[100];
} book;
How to Access Members
Once you define the structure, you can access it using a full stop, which is also called the member access operator. This operator is used as a period or break between the structure member and the variable name. Make sure to enter the variable name you want to access. You can define the variable of the entire structure using the struct keyword. Let us look at an example of how you can use structures:
#include <iostream>
#include <cstring>
using namespace std;
struct Books {
int book_id;
char book_title[50];
char genre[50];
char author[100];
};
int main() {
struct Books Book1;
// This is where you declare the variable Book1 in the Book structure
struct Books Book2; // This is where you declare the variable Book2 in the Book structure
// Let us now look at how you can specify the details of the first variable
Book1.book_id = 120000;
strcpy( Book1.book_title, "Harry Potter and the Philosopher’s Stone");
strcpy( Book1.genre, "Fiction");
strcpy( Book1.author, "JK Rowling");
// Let us now look at how you can specify the details of the second variable
Book2.book_id = 130000;
strcpy( Book2.book_title, "Harry Potter and the Chamber of Secrets");
strcpy( Book2.genre, "Fiction");
strcpy( Book2.author, "JK Rowling");
// The next statements are to print the details of the first and second variables in the structure
cout << "Book 1 id: " << Book1.book_id <<endl;
cout << "Book 1 title: " << Book1.book_title <<endl;
cout << "Book 1 genre: " << Book1.genre <<endl;
cout << "Book 1 author: " << Book1.author <<endl;
cout << "Book 2 id: " << Book2.book_id <<endl;
cout << "Book 2 title: " << Book2.book_title <<endl;
cout << "Book 2 genre: " << Book2.genre <<endl
;
cout << "Book 2 author: " << Book2.author <<endl;
return 0;
}
The code above will give you the following output:
Book 1 id: 120000
Book 1 title: Harry Potter and the Philosopher’s Stone
Book 1 genre: Fiction
Book 1 author: JK Rowling
Book 2 id: 130000
Book 2 title: Harry Potter and the Chamber of Secrets
Book 2 genre: Fiction
Book 2 author: JK Rowling
Using Structures as Arguments
You can use structures as arguments in a function similar to how you pass a pointer or variable as part of the function. You need to access the variables in the structure in the same way as we did in the example above.
#include <iostream>
#include <cstring>
using namespace std;
void printBook( struct Books book );
struct Books {
int book_id;
char book_title[50];
char genre[50];
char author[100];
};
int main() {
struct Books Book1; // This is where you declare the variable Book1 in the Book structure
struct Books Book2; // This is where you declare the variable Book2 in the Book structure
// Let us now look at how you can specify the details of the first variable
Book1.book_id = 120000;
strcpy( Book1.book_title, "Harry Potter and the Philosopher’s Stone");
strcpy( Book1.genre, "Fiction");
strcpy( Book1.author, "JK Rowling");
// Let us now look at how you can specify the details of the second variable
Book2.book_id = 130000;
strcpy( Book2.book_title, "Harry Potter and the Chamber of Secrets");
strcpy( Book2.genre, "Fiction");
strcpy( Book2.author, "JK Rowling");
// The next statements are to print the details of the first and second variables in the structure
printBook( Book1 );
printBook( Book2 );
return 0;
}
void printBook(struct Books book )
{
cout << "Book id: " << book.book_id <<endl;
cout << "Book title: " << book.book_title <<endl;
cout << "Book genre: " << book.genre <<endl;
cout << "Book author: " << book.author<<endl;
}
When you compile the code written above, you receive the following output:
Book 1 id: 120000
Book 1 title: Harry Potter and the Philosopher’s Stone
Book 1 genre: Fiction
Book 1 author: JK Rowling
Book 2 id: 130000
Book 2 title: Harry Potter and the Chamber of Secrets
Book 2 genre: Fiction
Book 2 author: JK Rowling
Using Pointers
You can also refer to structures using pointers, and you can use a pointer similar to how you would define a pointer for regular variables.
struct Books *struct_pointer;
When you use the above statement, you can use the pointer variable defined to store the address of the variables in the structure.
struct_pointer = &Book1;
You can also use a pointer to access one or members of the structure. To do this, you need to use the -> operator:
struct_pointer->title;
Let us rewrite the example above to indicate a member or the entire structure using a pointer.
#include <iostream>
#include <cstring>
using namespace std;
void printBook( struct Books *book );
struct Books {
int book_id;
char book_title[50];
char genre[50];
char author[100];
};
int main() {
struct Books Book1; // This is where you declare the variable Book1 in the Book structure
struct Books Book2; // This is where you declare the variable Book2 in the Book structure
// Let us now look at how you can specify the details of the first variable
Book1.book_id = 120000;
strcpy( Book1.book_title, "Harry Potter and the Philosopher’s Stone");
strcpy( Book1.genre, "Fiction");
strcpy( Book1.author, "JK Rowling");
// Let us now look at how you can specify the details of the second variable
Book2.book_id = 130000
;
strcpy( Book2.book_title, "Harry Potter and the Chamber of Secrets");
strcpy( Book2.genre, "Fiction");
strcpy( Book2.author, "JK Rowling");
// The next statements are to print the details of the first and second variables in the structure
printBook( Book1 );
printBook( Book2 );
return 0;
}
// We will now use a function to accept a structure pointer as its parameter.
void printBook( struct Books *book ) {
cout << "Book id: " << book->book_id <<endl;
cout << "Book title: " << book->book_title <<endl;
cout << "Book genre: " << book->genre<<endl;
cout << "Book author: " << book->author <<endl;
}
When you write the above code, you obtain the following output:
Book id: 120000
Book title: Harry Potter and the Philosopher’s Stone
Book genre: Fiction
Book author: JK Rowling
Book id: 130000
Book title: Harry Potter and the Chamber of Secrets
Book genre: Fiction
Book author: JK Rowling
Typedef Keyword
If the above methods are a little tricky for you, you can use an alias type to define a structure. For instance,
typedef struct {
int book_id;
char book_title[50];
char genre[50];
char author[100];
} Books;
This is an easier syntax to use since you can directly define all the variables in the structure without using the keyword ‘struct.’
Books Book1, Book2;
You do not have to use a typedef key only to define a structure. It can also be used to define regular variables.
typedef long int *pint32;
pint32 x, y, z;
The type long ints point to the variables x, y and z.