TExcelTAD
Home › C++ › C++ Structures

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

cpp · try it yourself
#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:

cpp · try it yourself
#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:

cpp · try it yourself
#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.

cpp · try it yourself
#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 struct is really a class whose members are public by default. Use struct for simple bundles of data and class when 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?

  1. b->year
  2. b::year
  3. b.year
  4. b[year]

What is commonly forgotten at the end of a struct definition?

  1. A colon
  2. A semicolon after }
  3. The return type
  4. The keyword end

After Rect copy = small; copy.w = 100;, what happens to small.w?

  1. It also becomes 100
  2. It stays unchanged
  3. It becomes 0
  4. Compile error