π¦ C++ Arrays & Structures β Complete Guide to Arrays, Pointers, Structures, Unions & Enums
π§² Introduction β Why Use Arrays and Structures in C++
Arrays and structures in C++ allow you to organize and store collections of data efficiently. Arrays let you work with multiple values of the same type, while structures enable the grouping of different types under one name. Understanding how to use arrays, pointers, unions, and enumerations is fundamental to building scalable and modular C++ programs.
π― In this guide, youβll learn:
- How arrays (1D, 2D) store data and interact with functions
- What pointers to arrays are and why theyβre useful
- The role of structures and unions in data modeling
- The difference between traditional enums and enum class
π Topics Covered
| πΉ Topic | π Description | 
|---|---|
| π C++ Arrays | One-dimensional, two-dimensional arrays, passing and returning arrays | 
| π Pointer to Arrays | How to use pointers with arrays for memory-efficient access | 
| π§± C++ Structures | Create user-defined types with multiple related fields | 
| π C++ Unions | Efficient memory-sharing data types with mutually exclusive fields | 
| π¨ C++ Enums & Enum Class | Define readable, scoped sets of named integer constants | 
π C++ Arrays β 1D, 2D, Passing, Returning
β What is an Array?
An array is a fixed-size, index-based container that holds multiple elements of the same data type.
πΉ 1D Array Example
int numbers[5] = {1, 2, 3, 4, 5};
Access using numbers[0], numbers[1], etc.
πΈ 2D Array Example
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
Acts like a table (rows Γ columns): matrix[1][2] = 6
π€ Passing Arrays to Functions
void display(int arr[], int size) {
    for (int i = 0; i < size; i++)
        cout << arr[i] << " ";
}
Arrays are passed by reference, not by value.
π Returning Arrays from Functions
Return a pointer or use std::array:
int* getArray() {
    static int arr[3] = {10, 20, 30};
    return arr;
}
π C++ Pointer to Arrays
A pointer to an array allows more flexibility in managing memory and traversing arrays.
π§ͺ Syntax Example
int arr[10];
int (*ptr)[10] = &arr;
Here, ptr points to the whole array arr.
Pointers to arrays are commonly used in dynamic memory allocation and function arguments.
π§± C++ Structures
Structures group variables of different data types under one name. Ideal for modeling real-world entities.
π¦ Example
struct Employee {
    int id;
    string name;
    float salary;
};
π§ Usage
Employee e1 = {101, "John", 55000.0};
cout << e1.name;
Structures can be nested and passed to functions.
π C++ Unions
Unions allow multiple variables to occupy the same memory location, useful for saving memory.
π§ͺ Syntax Example
union Data {
    int i;
    float f;
    char str[20];
};
Only one member can be used at a time.
Data d;
d.i = 10;
cout << d.i;
Unions are mostly used in systems-level programming and embedded systems.
π¨ C++ Enums β Enum Class
πΈ Traditional Enum
enum Color { Red, Green, Blue };
All values are in the global namespace.
β Scoped Enum (Enum Class)
enum class Color { Red, Green, Blue };
Access with scope resolution:
Color c = Color::Green;
Enum classes provide better type safety and avoid name conflicts.
π Summary β Recap & Next Steps
π Key Takeaways:
- Arrays allow you to store and access multiple values using indices
- Structures group diverse data into a single logical unit
- Pointers to arrays and unions offer memory-efficient techniques
- enum classprovides scoped enumerations for clean code
C++ offers powerful tools to manage both homogeneous and heterogeneous data through arrays, structures, unions, and enums. These elements are the building blocks for more advanced programming techniques like file handling, data modeling, and memory management. Understanding and using them effectively can significantly improve your codeβs readability and performance.
β FAQs β C++ Arrays & Structures
β Can I return an array from a C++ function?
β
 Yes, typically via pointers or using std::array or std::vector.
β What is the main difference between a structure and a class?
β
 In C++, structures have public members by default, whereas classes have private members.
β When should I use unions instead of structures?
β
 Use unions when you want to store one of many variables in the same memory space, not all at once.
β What are the benefits of enum class?
β
 Scoped enums prevent naming collisions and are type-safe.
β Can arrays be passed by value in C++?
β
 No. Arrays are always passed by reference unless wrapped in another type like std::array.
Share Now :
