Structures
C++ Structures (struct)
Definition: A structure (often called a struct) is a user-defined data type that allows you to group multiple related variables of different data types together into a single, cohesive unit. The individual variables inside a structure are referred to as its members or fields.
Why: While arrays allow you to group multiple values of the same data type, real-world data is rarely uniform. For instance, a student profile requires a combination of text (name), integers (age), and decimals (marks). Structures provide the fundamental blueprint needed to model complex, real-world entities cleanly before transitioning into full Object-Oriented Programming.
Declaration Syntax
To define a structure, use the struct keyword followed by a custom name. Inside the curly braces, list the variables that make up the structure. CRITICAL: You must always terminate a structure definition with a semicolon (;) after the closing brace.
struct StructureName {
dataType member1;
dataType member2;
dataType member3;
};
Example: Modeling a Student Record
The following program defines a Student structure, creates an instance of it, assigns values to its members using the dot (.) operator, and prints the record:
#include <iostream> #include <string> using namespace std; // Defining the structural blueprint struct Student { string name; int age; float marks; }; int main() { // Creating a structure variable named s1 Student s1; // Assigning values to members using the dot operator s1.name = "Anu"; s1.age = 19; s1.marks = 88.5f; // Accessing and displaying structure data cout << "Name: " << s1.name << endl; cout << "Age: " << s1.age << endl; cout << "Marks: " << s1.marks << endl; return 0; }
Arrays vs. Structures
| Feature | Array | Structure (struct) |
|---|---|---|
| Data Types | Homogeneous (All elements must match). | Heterogeneous (Can mix string, int, float, etc.). |
| Access Method | Accessed via a zero-based numeric index (e.g., arr[0]). |
Accessed via named members using the dot operator (e.g., s1.name). |
| Nature | Built-in collection sequence. | User-defined custom data type blueprint. |
Key Notes
- Semicolon Rule: Forgetting the closing semicolon after the struct's closing curly brace (
};) is one of the most notorious compiler errors for beginners. Always double-check this layout! - Arrays of Structures: You can combine these concepts to create arrays of structs. For example,
Student classroom[50];creates a collection of 50 individual student records, which you can easily loop through. - Struct vs. Class: In C++, structures and classes are almost identical. The only technical difference is access control: members of a
structare **public by default**, meaning they can be modified directly from outside the structure, while members of aclassare **private by default**. - Initialization Shorthand: You can initialize a structure on a single line using brace-enclosed lists matching the declaration order, like this:
Student s2 = {"Rohan", 20, 91.2f};.
🏋️ Test Yourself With Exercises
Take our quiz on Structures to test your knowledge.
Browse Quizzes »