Structures
- In-depth video on basic syntax, section on structures
- Video on programming techniques that explains how structures work
Concepts
Section titled “Concepts”- User-defined data types
- A structure for grouping related variables
- A structure as an abstraction (a computer lab consists of desks and computers)
- Field
- Structure initialization
- Pointer to a structure
- The
->operator
Examples for understanding
Section titled “Examples for understanding”Analyze what happens in the examples:
1. Structure initialization
Section titled “1. Structure initialization”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A a { 1, 2 }; std::cout << a.f1 << std::endl; std::cout << a.f2 << std::endl;}Execution trace
Answer:
By default, brace initialization syntax can be used for any structure. It assigns values to the fields one by one, in the order in which they are declared.
It prints 1 and 2.
2. An expression of a structure type
Section titled “2. An expression of a structure type”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A a = A{ 1, 2 }; std::cout << a.f1 << std::endl; std::cout << a.f2 << std::endl;}Execution trace
Answer:
The same thing happens as in the previous example.
What is the type of the expression A{ 1, 2 }?
The type of a variable does not have to be an integer type (int, uint8_t);
it can also be user-defined.
In this example, A is a user-defined data type.
A{ 1, 2 } is an expression of type A.
More precisely, the static (known at compile time) type of the value
obtained by evaluating the expression A{ 1, 2 } is A.
How can a type be something other than int?
You can think of such an expression as a piece of memory with two fields, f1 and f2,
whose values are 1 and 2, as in the image below.
Think of it as temporary data simply suspended in the air; it is not stored
anywhere in RAM.

The fact that the expression has type A means that
the result of evaluating it can be stored in a variable of type A.
Now this “suspended” value is placed in variable a.
It can be placed there because a has type A,
which is compatible with the type of expression A{ 1, 2 } (also type A).
3. Assigning a structure to an int
Section titled “3. Assigning a structure to an int”#include <iostream>
struct A{ int f1; int f2;};
int main(){ int a = A{ 1, 2 }; std::cout << a << std::endl;}Answer:
It will not compile because the expression A{ 1, 2 } has type A, while a has type int.
A value of type A cannot be stored in a variable of type int.
4. Operations with fields
Section titled “4. Operations with fields”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A a; a.f1 = 5; int b = a.f1; a.f2 = b;
std::cout << a.f1 << std::endl; std::cout << a.f2 << std::endl; std::cout << b << std::endl;}Execution trace
Answer:
You can read from and write to each field individually.
It prints 5 three times.
5. Assigning a field from a temporary structure
Section titled “5. Assigning a field from a temporary structure”#include <iostream>
struct A{ int f1; int f2;};
int main(){ int a = A{ 1, 2 }.f1; std::cout << a << std::endl;}Answer:
It compiles and prints 1.
Here, a is initialized with the result of the expression A{ 1, 2 }.f1.
The expression consists of A{ 1, 2 }, which evaluates to a temporary value of type A,
followed by .f1, which accesses the f1 field of that temporary value.
That is, it first creates a temporary A with f1 = 1, f2 = 2 — think of it as simply suspended in the air —
and then reads only f1 from it.
The expression A{ 1, 2 }.f1 has type int.
In other words, it evaluates to the value of the f1 field of that temporary A.
That value, 1, is then stored in variable a.
6. Assigning a structure
Section titled “6. Assigning a structure”#include <iostream>
struct A{ int f;};int main(){ A a{ 1 }; A b{ a }; a.f = 2; std::cout << a.f << std::endl; std::cout << b.f << std::endl;}Execution trace
Answer:
A a{ 1 } creates a variable a with field f = 1.
A b{ a } creates a new variable b and copies the values of all fields from a into it.
At this point b.f becomes 1, but as a separate copy in b’s own memory.
So a.f = 2 changes only a, while b stays untouched.
It prints 2 and 1.
7. Field addresses
Section titled “7. Field addresses”#include <iostream>
struct A{ int f;};
int main(){ A a; int* b = &a.f; *b = 5; std::cout << a.f << std::endl;}Execution trace
Answer:
You can obtain the address of a field inside a variable of a structure type.
In the line int* b = &a.f, the dot in &a.f is evaluated first,
giving access to field f inside a; its address is then obtained using &.
Value 5 is stored in a.f.
8. Assigning a structure variable (1)
Section titled “8. Assigning a structure variable (1)”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A a; a.f1 = 5; a.f2 = 6;
A b; b = a;
std::cout << a.f1 << std::endl; std::cout << a.f2 << std::endl;
std::cout << b.f1 << std::endl; std::cout << b.f2 << std::endl;}Execution trace
Answer:
b = a copies the values of all fields of a into b.
9. Assigning a structure variable (2)
Section titled “9. Assigning a structure variable (2)”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A a; a.f1 = 5; a.f2 = 6;
A b; b.f1 = 7; b = a;
std::cout << a.f1 << std::endl; std::cout << a.f2 << std::endl;
std::cout << b.f1 << std::endl; std::cout << b.f2 << std::endl;}Execution trace
Answer:
b = a does not know which fields have already been initialized.
It copies all fields indiscriminately.
In the end, b has f1 = 5, f2 = 6.
10. Structure address (1)
Section titled “10. Structure address (1)”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A a; a.f1 = 5; a.f2 = 6; A* pa = &a; A b = *pa;
std::cout << b.f1 << std::endl; std::cout << b.f2 << std::endl;}Execution trace
Answer:
In the initialization of b, *pa is essentially equivalent to directly accessing a.
This is the same situation as above.
11. Structure address (2)
Section titled “11. Structure address (2)”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A a; a.f1 = 5; a.f2 = 6; A* pa = &a; a.f1 = 7; A b = *pa;
std::cout << b.f1 << std::endl; std::cout << b.f2 << std::endl;}Execution trace
Answer:
b.f1 receives 7.
The & operator does not take the address of the values f1 = 5, f2 = 6;
it takes the address of variable a.
When dereferencing that address, you always get the current value of a.
12. The -> operator
Section titled “12. The -> operator”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A a; a.f1 = 5; A* pa = &a; (*pa).f2 = 6; pa->f1 = 7;
std::cout << a.f1 << std::endl; std::cout << a.f2 << std::endl;}Execution trace
Answer:
*pa is essentially equivalent to directly accessing a.
(*pa).f2 = 6 -> a.f2 = 6.
pa->f1 means “go to the variable at the address in pa, then access field f1.”
This can also be written as (*pa).f1.
In practice, it is equivalent to a.f1.
13. Dispute a claim about the pointer address
Section titled “13. Dispute a claim about the pointer address”A student claims that taking the address of a gives access to the structure with ->.
Do you agree?
struct A{ int field;};
A value{ 5 };A* a{ &value };(&a)->field = 8;Answer
It does not compile.
a has type A*, but &a has type A**: it points to a pointer variable, which has no field.
Use a->field = 8; or (*(&a))->field = 8;.
14. Overwriting through an address
Section titled “14. Overwriting through an address”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A a{}; A* pa { &a }; *pa = { 5, 6 }; std::cout << pa->f1 << std::endl; std::cout << pa->f2 << std::endl;}Execution trace
Answer
*fp = { 5, 6 } -> *(&a) = { 5, 6 } -> a = { 5, 6 }
This means a.f1 = 5, a.f2 = 6.
fp->f1 reads what is currently in a. Since 5 was written there earlier, it prints 5.
Similarly, fp->f2 prints 6.
15. Address of a structure type
Section titled “15. Address of a structure type”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A* fp { &A }; *fp = { 5, 6 }; std::cout << fp->f1 << std::endl;}Answer:
This is not allowed because structure A itself stores no data.
Data can be stored in a variable of type A, which must be created first.
16. Address of a type field
Section titled “16. Address of a type field”#include <iostream>
struct A{ int f1; int f2;};
int main(){ int* fp { &A.f1 }; *fp = 5; std::cout << *fp << std::endl;}Answer:
This is not allowed. The explanation is the same as in the previous example.
17. An address field
Section titled “17. An address field”#include <iostream>
struct A{ int* f1;};
int main(){ int num = 1; A a; a.f1 = # *a.f1 = 2;
std::cout << a.f1 << std::endl; std::cout << num << std::endl;}Execution trace
Answer:
In *a.f1, * is applied after ., that is:
*a.f1 -> *(&num) -> num.
After that, num is overwritten with 2.
It prints the address of num, then 2.
18. A nested structure
Section titled “18. A nested structure”#include <iostream>
struct Nested{ int f;};
struct A{ Nested nested; int f;};
int main(){ A a { .nested = { .f = 1 }, .f = 2, };
a.f = 3; a.nested = { 5 }; a.nested.f = 6;
std::cout << a.f << std::endl; std::cout << a.nested.f << std::endl;}Execution trace
Answer:
Other structures can be nested inside a structure. This is called nesting, and it is used constantly in programming.
Here, in the end, a.f equals 3, and a.nested.f equals 6.
19. Copying a nested structure
Section titled “19. Copying a nested structure”#include <iostream>
struct Nested{ int f;};
struct A{ Nested nested; int f;};
int main(){ A a { .nested = { 1 }, .f = 2 }; A b { a };
a.nested.f = 3; a.f = 4;
std::cout << b.nested.f << std::endl; std::cout << b.f << std::endl; std::cout << a.nested.f << std::endl; std::cout << a.f << std::endl;}Execution trace
Answer:
A b{ a } copies the values of all fields, including the whole nested structure.
At the moment of copying, a holds nested.f = 1, f = 2,
so b receives the same values: b.nested.f = 1, b.f = 2.
But it is a separate copy in b’s own memory: the Nested structure itself
is stored inside A by value, not by address.
So a.nested.f = 3 and a.f = 4
affect only a, while b stays untouched.
It prints 1, 2, 3, 4.
20. Why will this code not compile?
Section titled “20. Why will this code not compile?”struct A{ int value; A other;};Answer:
A structure cannot contain itself because it would then occupy an infinite amount of memory. It is possible to include a pointer to another such structure, because its size does not depend on the size of the structure.
21. Linked list
Section titled “21. Linked list”#include <iostream>
struct Node{ int value; Node* next;};
int main(){ Node end{}; end.value = 1; end.next = nullptr;
Node start{}; start.value = 2; start.next = &end;
Node* current = &start; std::cout << current->value << std::endl;
current = current->next; std::cout << current->value << std::endl;
current = current->next; std::cout << current->value << std::endl;}Answer:
This creates a linked list, a data structure used very often in programming.
Each node holds a pointer to another node of the same type.
The null pointer in the last node of the list (nullptr) marks the end of the list.
The code prints 2, then 1, and then crashes on the last
line when it dereferences a null pointer (segmentation fault).
22. Structure size (1)
Section titled “22. Structure size (1)”#include <iostream>
struct A{ int f1; int f2;};
int main(){ A a; std::cout << sizeof(a) << std::endl; std::cout << sizeof(A) << std::endl;}Answer:
sizeof(a) gives the size of a variable in bytes.
sizeof(A) gives the size in bytes of a variable of type A, if one were created.
Both forms are equivalent.
The result is 8 because each structure has 2 ints, each of which occupies 4 bytes.
23. Field address from a structure address
Section titled “23. Field address from a structure address”#include <iostream>
struct A{ int f;};
int main(){ A a { 1 }; A* pa { &a }; int* pf { &pa->f }; *pf = 2; std::cout << a.f << std::endl;}Execution trace
Answer:
This demonstrates that you can take the address of a field after applying ->.
& is applied after ->, just as it is after ..
&pa->f -> &(pa->f) -> &((*pa).f) -> &(a.f)
It prints 2.
24. (advanced level): Structure size (3)
Section titled “24. (advanced level): Structure size (3)”#include <iostream>
struct A{ uint8_t f1; int f2; uint8_t f3;};
int main(){ std::cout << sizeof(A) << std::endl;}Answer:
This is where alignment comes into play. Alignment leaves empty spaces between fields. This is done because it allows the processor to read data from memory faster.
First, the largest field size is determined; it is usually no more than 16 bytes.
In this example, it is int — 4 bytes.
Now divide memory into 4-byte slots. If the next field does not fit entirely into the remaining space in a 4-byte slot, it goes into the next one.
uint8_t f1goes into the first byte of the first slot;int f2does not fit in the first slot afterf1, so it goes into the next one. The remaining 3 bytes of the first slot are unused (padding bytes);uint8_t f3goes into the third slot;- The remaining 3 bytes of the third slot are unused.
In total, this gives 3 slots of 4 bytes each.
If a structure contains a field of another structure type, its slot is no smaller than the slots of that nested structure.
Alignment can be disabled using #pragma pack.
25. (advanced level): Structure size (4)
Section titled “25. (advanced level): Structure size (4)”#include <iostream>
struct A{};
int main(){ std::cout << sizeof(A) << std::endl;}Answer:
It prints 1.
According to the C++ standard, the size of an object cannot be less than 1, so that 2 objects of this type can be distinguished from each other. The reason is that 2 objects cannot have the same memory address.
Objects are covered in lab 13.
26. Copying a structure with a pointer field
Section titled “26. Copying a structure with a pointer field”#include <iostream>
struct A{ int* pa; int a;};
int main(){ A s1 { .pa = nullptr, .a = 1 }; s1.pa = &s1.a;
A s2 { s1 }; *s2.pa = 2;
std::cout << s1.a << std::endl; std::cout << s2.a << std::endl;}Execution trace
Answer:
A s2{ s1 } copies the values of all fields, but for the pointer
it copies the address itself, not the data at that address.
This is called a shallow copy.
At the moment of copying, s1 holds a = 1, pa = &s1.a,
so s2 receives the same values: s2.a = 1, s2.pa = &s1.a.
Note: s2.pa points to s1.a, not to s2.a.
Therefore *s2.pa = 2 follows the address &s1.a and overwrites s1.a,
while s2.a stays untouched.
It prints 2, 1.
Type-checking puzzles
Section titled “Type-checking puzzles”Each snippet is independent. Does it compile? Give the types of both sides of the last assignment. Most snippets are deliberately incorrect; explain a correction instead of trying to run them.
1. A structure pointer assigned to a structure
Section titled “1. A structure pointer assigned to a structure”struct A{ int field;};
A value{ 5 };A* p{ &value };A copy{};copy = p;Execution trace
Answer
It does not compile.
p has type A*, while copy has type A.
Use copy = *p; to copy the structure.
2. A structure address assigned to a structure
Section titled “2. A structure address assigned to a structure”struct A{ int field;};
A value{ 5 };A copy{};copy = &value;Execution trace
Answer
It does not compile.
&value has type A*, not A.
Use copy = value; to copy the structure value.
3. A structure assigned to its pointer
Section titled “3. A structure assigned to its pointer”struct A{ int field;};
A value{ 5 };A* p{};p = value;Execution trace
Answer
It does not compile.
value has type A, while p requires A*.
Use p = &value; to store the address.
4. A structure assigned to a double pointer
Section titled “4. A structure assigned to a double pointer”struct A{ int field;};
A value{ 5 };A** pp{};pp = value;Execution trace
Answer
It does not compile.
value has type A, not A**.
To get an A**, create an A* variable and take its address.
5. A double pointer assigned to a structure
Section titled “5. A double pointer assigned to a structure”struct A{ int field;};
A value{ 5 };A* p{ &value };A** pp{ &p };A copy{};copy = pp;Execution trace
Answer
It does not compile.
pp has type A**, not A.
Use copy = **pp; to copy the structure reached through both pointers.
6. An integer pointer assigned to a structure pointer
Section titled “6. An integer pointer assigned to a structure pointer”struct A{ int field;};
A value{ 5 };int* fieldAddress{ &value.field };A* p{};p = fieldAddress;Execution trace
Answer
It does not compile.
fieldAddress has type int*; p requires A*.
The field address and the structure address have different types.
7. A structure pointer assigned to an integer pointer
Section titled “7. A structure pointer assigned to an integer pointer”struct A{ int field;};
A value{ 5 };A* p{ &value };int* fieldAddress{};fieldAddress = p;Execution trace
Answer
It does not compile.
p has type A*, not int*.
Use fieldAddress = &p->field; to address the integer field.
8. Copying the structure through two pointers
Section titled “8. Copying the structure through two pointers”struct A{ int field;};
A value{ 5 };A* p{ &value };A** pp{ &p };A copy{};copy = **pp;Execution trace
Answer
It compiles.
**pp has type A, so this copies the structure.
copy.field becomes 5; changing it later does not change value.field.
9. Assigning an integer through a structure pointer
Section titled “9. Assigning an integer through a structure pointer”struct A{ int field;};
A value{ 5 };A* p{ &value };*p = 8;Execution trace
Answer
It does not compile.
*p accesses an A, not an int.
Use p->field = 8; to change the integer field.
10. An integer assigned from a structure field
Section titled “10. An integer assigned from a structure field”struct A{ int field;};
A value{ 5 };A* p{ &value };int result{};result = p->field;Execution trace
Answer
It compiles.
p->field has type int, so the assignment is valid.
result becomes 5.
Assignment
Section titled “Assignment”Explain in words what happens in the
memory_example_2 example.
You can copy the code file and write comments directly in the code explaining what happens.
Use the Excel table from the example to visualize the memory layout.
You do not necessarily have to comment on every step; you can instead explain what is printed at each stage, and why (what a given pointer points to at a given moment, what is currently stored in memory, and so on). You can also use a debugger for a better understanding.