C++ Tutorial
Estimated reading: 4 minutes 45 views

πŸ“¦ 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++ ArraysOne-dimensional, two-dimensional arrays, passing and returning arrays
πŸ“ Pointer to ArraysHow to use pointers with arrays for memory-efficient access
🧱 C++ StructuresCreate user-defined types with multiple related fields
πŸ”„ C++ UnionsEfficient memory-sharing data types with mutually exclusive fields
🎨 C++ Enums & Enum ClassDefine 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 class provides 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ“¦ C++ Arrays & Structures

Or Copy Link

CONTENTS
Scroll to Top