C++20 sections · 1024 units
Open in Course

Defining a Struct

How to create one

Define a struct with the struct keyword, a name, and member variables inside curly braces. End with a semicolon. Each struct variable you create gets its own copy of these members.

Example: struct Point { int x; int y; };. This creates a Point type with two integers. The struct name is Point. The members are x and y. The semicolon at the end is required.

Define structs at the top of your file or in a header, outside any function. This makes the type available everywhere. Once defined, Point works like any built-in type.