← Back to C++ Course | Chapter 8: OOP Core | Lesson 13 of 14

C++ Structures

A struct groups several related values under one name, like a form with different blanks for name, age and grade. It is a simple way to bundle information.
Syntax
cpp
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?

cpp
// 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;
}

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

cpp
// 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;
}

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++

cpp
#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;
}

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

cpp
// 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;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the semicolon after the closing brace of a struct.
  2. Comparing two structs with ==, which is not defined by default.
  3. Forgetting that struct members are public by default.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.