Day 68 of 360: Mastering Structures and Union.

Prashant JoshiPrashant Joshi
2 min read

Today, I delved deep into structures and unions in C++, focusing on how to create them and understanding their memory assignments. This is essential for efficient data management in complex applications.

Understanding Structures in C++

**

Structures in C++ are user-defined data types that allow you to group different types of data together. Think of a structure as a container that can hold various data types, unlike arrays, which only store data of the same type.**

Here’s an example of defining a structure in C++:

1724178321959?e=1729728000&v=beta&t=OCnDJ7x1CP0Kjxd4qbvfzQ-rfPpHDa8d8Zy4dNB-a9I

In this example, we’ve defined a structure called employee that contains three fields: eId (an integer), favChar (a character), and salary (a float).

Once a structure is defined, you can create instances of it like so:

1724178410043?e=1729728000&v=beta&t=Jr7CcuYbcUaYk7qyjPmqfnU-12vBBwMUqeeq-PO1kAY

Here, we've created an instance of the employee structure named harry, assigned values to its fields, and then printed these values.

Using typedef with Structures

To simplify the usage of structures, C++ provides the typedef keyword, allowing you to define an alias for the structure, making the code cleaner:

1724178507927?e=1729728000&v=beta&t=SbonTxoKljtBV2kG43XpW_uf3XmFHQ5tNvgDvFuUiss

With typedef, you can now create structure variables without the struct keyword:

1724178546233?e=1729728000&v=beta&t=V2jYDXeT_mVWhW6v9f20vtPZzbvpGA0vXlx8EzOIBWM

In this case, ep is used as an alias for struct employee, making the code more concise.

**

Understanding Unions in C++**

Unions are similar to structures but with a key difference: they provide better memory management by sharing the same memory location for all its members. This means only one member can hold a value at any time.

Here’s how you define a union:

1724178588760?e=1729728000&v=beta&t=LH5fChN8BrgBwU-4oQ4qRFU7sOrUbZAZYe8Ouio483M

This money union can store either an integer (rice), a character (car), or a float (pounds), but only one at a time. Here’s an example of using a union:

1724178607640?e=1729728000&v=beta&t=00iMIXQwuwaMAlLEUJK4mujlyw0aNLLJv51_nAr0n5s

In this code, we've assigned a value to rice and printed it. If you try to access another member after assigning a value, you might get unexpected results (garbage values) because all members share the same memory location.

Follow My Journey:

Stay tuned for more updates as I continue to build my skills and work towards my DevOps goals!

0
Subscribe to my newsletter

Read articles from Prashant Joshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prashant Joshi
Prashant Joshi

Fuelled by boundless passion for DevOps and cloud technologies ☁️☁️☁️ ☁️, I'm on an exhilarating journey of mastery of Computer Application (MCA). I'm not just learning; I'm devouring DevOps principles and cloud tech, crafting the future through relentless innovation and automation. 😉