Structures

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. Structures

What is a structure?

A structure is a type of variable that is made up of other variables. It groups variables into one variable.

Defining a structure

Let us say we want to store a date in the memory. We could do the following:

int year = 2000; int month = 1; int day = 31;

However, there are better ways of doing it. We could define a structure called date that would store the year, the month and the day of the month, all in one variable. We define a structure by writing the keyword struct and then the name of the structure followed by the definition of the variables composing it, inside braces {} ended by a semicolon ;. The variables inside a structure are called members. Here we create a structure to represent a date:

1
2
3
4
5
6
7
8
9
10
11
struct Date { int year; int month; int day; }; int main(){ struct Date birthday; // Here, the keyword 'struct' is required only in C, not in C++. return 0; }

Above, we created a variable of type struct Date named birthday. That variable is made of 3 members of type int. We can access these variables by putting a point followed by the name of the member we want to access after the name of the variable of the structure type. Example:

birthday.year = 1900; birthday.month = 5; birthday.day = 21;

It is possible to give default values to the members of a structure. Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Date { int year=2000; int month=11; int day=11; }; void main(){ struct Date birthday; // In C++, we can simply write 'Date birthday;'. std::cout << birthday.year << std::endl; // “2000” is printed in the console. return 0; }

In the code above, since we gave default values to the members of the structure Date, when we created the variable birthday, its members as been automatically assigned values.

Note that an instance of a structure (A variable which type is a structure type) is called an object of that structure. In the example above, birthday is an object of Date.

Pointers of structure type

Pointers of structure type work the same way pointers of primitive type (like int) work. However, to access the members of an object through a pointer, we must use -> instead of a point .. Example:

1
2
3
4
5
6
7
8
9
void main(){ struct Date birthday; struct Date * ptr = &birthday; ptr->year = 1990; // We access the member “year” using “->” return 0; }

The following line would have caused a compilation error:

ptr.year = 1990; // Error: It does not work because “ptr” is a pointer. We must use “->”.

Objects inside a structure

Structures can have members that are objects. It works the same way as with primitives (int, float, unsigned int…). Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
struct Date { int year=2000; int month=1; int day=1; }; struct Time { int hours=12; int minutes=30; int seconds=43; }; struct Moment { struct Date date; struct Time time; }; int main(){ struct Moment rightNow; struct Time time; time.hours = 5; rightNow.time = time; rightNow.time.minutes = 45; rightNow.date.month = 7; return 0; }

Structures can, however, not contain objects of their type. Why? Because it would create an infinite loop:

struct A { struct A a; // Error. };

In the structure definition from above, the structure A would possess a member named a which possesses a member named a, which possesses a member named a, which possesses a member named a...

Structures can, however, contain pointers of their type:

struct A { struct A* a; // It works. };

Defining structures inside functions

In the examples above, structures were defined outside of the main function. However, it is possible to define a structure inside a function. Example:

1
2
3
4
5
6
7
8
9
void func() { struct MyStruct{ int member1; int member2; }; struct MyStruct myVariable; }

However, here, struct MyStruc only exists inside the function func. We could not define a variable of type struct MyStruct outside of the braces of func. Like variables, structure definitions have a life span.

You noticed the semicolon ; at the end of structure definitions? It is possible to define objects of that structure by putting names (separated by commas) before it. The following:

struct MyStruct{ int member1; int member2; }var, var2, var3;

Is equivalent to:

struct MyStruct{ int member1; int member2; }; struct MyStruct var, var2, var3;

It is recommended to define objects that way only when the structure is defined inside a function. Otherwise, it would create global variables (Variables that exist everywhere in the program since they are not defined inside braces) and we should never create global variables that are not constant.

Naming convention

In this course, structures are named in PascalCase. It is the same as camelCase, but the first letter is in uppercase.