C++ Structures
An array groups values of the same type. A structure (struct) groups related values of different types under one name. Each value inside is called a member. Structures are the first step towards designing your own types.
Declaring and using a struct
#include <iostream>
#include <string>
using namespace std;
struct Book {
string title;
string author;
int year;
double price;
};
int main() {
Book b1;
b1.title = "The Hobbit";
b1.author = "J.R.R. Tolkien";
b1.year = 1937;
b1.price = 9.99;
Book b2 = {"Dune", "Frank Herbert", 1965, 12.50};
cout << b1.title << " by " << b1.author << " (" << b1.year << ")\n";
cout << b2.title << " costs " << b2.price << "\n";
}The Hobbit by J.R.R. Tolkien (1937)
Dune costs 12.5
- The struct definition ends with a semicolon after the closing brace — easy to forget.
- Members are accessed with the dot operator:
b1.title. - A brace list initialises members in the order they are declared.
Default member values and designated initialisers
Members can have default values. C++20 also lets you name the members you initialise (designated initialisers), which makes long initialisations self-documenting. They must appear in declaration order:
#include <iostream>
#include <string>
using namespace std;
struct Settings {
int width = 800;
int height = 600;
bool fullscreen = false;
string title = "Untitled";
};
int main() {
Settings a; // all defaults
Settings b{.width = 1920, .height = 1080, .fullscreen = true};
cout << a.width << "x" << a.height << " " << a.title << "\n";
cout << b.width << "x" << b.height << " fullscreen=" << b.fullscreen
<< " " << b.title << "\n";
}800x600 Untitled
1920x1080 fullscreen=1 Untitled
Arrays of structs and nested structs
Structs combine nicely with arrays — and a struct may contain other structs:
#include <iostream>
#include <string>
using namespace std;
struct Point {
double x;
double y;
};
struct City {
string name;
Point location;
int population;
};
int main() {
City cities[] = {
{"Lumen", {1.0, 2.0}, 120000},
{"Brook", {4.5, -1.0}, 45000},
{"Ash", {0.0, 7.5}, 300000}
};
int total = 0;
for (const City& c : cities) {
cout << c.name << " at (" << c.location.x << ", " << c.location.y << ")\n";
total += c.population;
}
cout << "Total population: " << total << "\n";
}Lumen at (1, 2)
Brook at (4.5, -1)
Ash at (0, 7.5)
Total population: 465000
Copying structs and passing them to functions
Assigning one struct to another copies all members. Functions can take and return structs; pass large structs by const reference (const Book&) to avoid copying — references are explained in a later chapter.
#include <iostream>
using namespace std;
struct Rect {
int w, h;
};
int area(const Rect& r) {
return r.w * r.h;
}
Rect doubled(Rect r) { // r is a copy
r.w *= 2;
r.h *= 2;
return r;
}
int main() {
Rect small{3, 4};
Rect big = doubled(small);
cout << area(small) << " " << area(big) << "\n";
Rect copy = small;
copy.w = 100;
cout << small.w << " " << copy.w << "\n"; // the original is unchanged
}12 48
3 100
Note: In C++ a
structis really aclasswhose members are public by default. Usestructfor simple bundles of data andclasswhen you want to hide data behind functions — you will see this in the Classes section.
Test yourself
How do you access the member year of a struct variable b?
- b->year
- b::year
- b.year
- b[year]
What is commonly forgotten at the end of a struct definition?
- A colon
- A semicolon after
} - The return type
- The keyword
end
After Rect copy = small; copy.w = 100;, what happens to small.w?
- It also becomes 100
- It stays unchanged
- It becomes 0
- Compile error