C++ Structures
In this page:
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 <iostream>
struct Point {
int x;
int y;
};
int main() {
Point p = {3, 4};
std::cout << p.x << "," << p.y << std::endl;
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 <iostream>
struct Point {
int x;
int y;
};
int main() {
Point p1;
p1.x = 1;
p1.y = 2;
std::cout << p1.x << "," << p1.y << std::endl;
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 <iostream>
#include <string>
struct Address { std::string city; };
struct Person { std::string name; Address addr; };
int main() {
Person p = {"Alex", {"Delhi"}};
std::cout << p.name << " lives in " << p.addr.city << std::endl;
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
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: