C++ Structures
In this page:
struct StructName {
data_type member1;
data_type member2;
};
StructName variable_name = {value1, value2};
variable_name.member1;
What Is a Struct?
A struct in C++ groups related variables of possibly different types under one name, giving you a single unit that models a real-world record like a point, a date, or a book.
Unlike a plain array, each member has its own name and type, so you access fields by meaning (book.pages) instead of by numeric index.
Example: What Is a Struct?
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point {
int x;
int y;
};
// Program execution starts in main()
int main() {
Point p = {3, 4};
// Print to the console with cout
std::cout << p.x << "," << p.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Declaring and Using Structs
You declare a struct with the struct keyword followed by a name and a brace-enclosed list of member variables, then create instances the same way you'd declare any other type.
Dot notation (.) reaches a member on a struct value, while arrow notation (->) reaches a member through a pointer to a struct.
Example: Declaring and Using Structs
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point {
int x;
int y;
};
// Program execution starts in main()
int main() {
Point p1;
p1.x = 1;
p1.y = 2;
// Print to the console with cout
std::cout << p1.x << "," << p1.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structs vs Classes in C++
Unlike in C, a C++ struct can have constructors, member functions, and even inheritance -- the only real difference from a class is that struct members default to public while class members default to private.
Convention still uses struct for simple data-holding types and class for types with real behavior and invariants to protect, even though the compiler treats them almost identically.
Example: Structs vs Classes in C++
#include <iostream>
struct Point {
int x;
Point(int px) : x(px) {} // structs can have constructors too
};
int main() {
Point p(5); // only real difference from class: public by default
std::cout << p.x << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nesting and Arrays of Structs
Structs can contain other structs as members, letting you build up composite types like an Address inside a Person, and you can declare arrays of structs to hold many records of the same shape in one contiguous block.
This is the same pattern used to model rows of tabular data before reaching for a full class hierarchy.
Example: Nesting and Arrays of Structs
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Define a structure type named Address
struct Address { std::string city; };
// Define a structure type named Person
struct Person { std::string name; Address addr; };
// Program execution starts in main()
int main() {
Person p = {"Alex", {"Delhi"}};
// Print to the console with cout
std::cout << p.name << " lives in " << p.addr.city << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Passing Structs to Functions
Passing a struct by value copies every member, which is wasteful for large structs, so functions typically take a const StructType& reference to read data without copying or a plain reference to modify the caller's struct in place.
Choosing the right passing style here is the same performance consideration that applies to passing any sizeable object in C++.
Example: Passing Structs to Functions
#include <iostream>
struct Point { int x; int y; };
void printPoint(const Point &p) { // avoids copying every member
std::cout << p.x << "," << p.y << std::endl;
}
int main() {
Point p = {3, 4};
printPoint(p);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting the semicolon after the closing brace of a struct.
- Comparing two structs with
==, which is not defined by default. - Forgetting that struct members are public by default.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: