KEMBAR78
Chapter 8 - Structures and Unions | PDF | Variable (Computer Science) | Namespace
0% found this document useful (0 votes)
40 views16 pages

Chapter 8 - Structures and Unions

Chapter 8 of the document introduces structures and unions in C++. Structures are user-defined data types that group different variables together, while unions allow different variables to share the same memory space. Key differences include memory allocation, data manipulation, and usage scenarios for each data type.

Uploaded by

ihpaa1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views16 pages

Chapter 8 - Structures and Unions

Chapter 8 of the document introduces structures and unions in C++. Structures are user-defined data types that group different variables together, while unions allow different variables to share the same memory space. Key differences include memory allocation, data manipulation, and usage scenarios for each data type.

Uploaded by

ihpaa1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

BIT1C013

INTRODUCTION TO PROGRAMMING

Chapter 8
Structures and Unions
Learning Objectives
At the end of the session, student should be able to:

▪ Understand the concepts of structure and union


▪ Develop simple program using structure and union
▪ Able to differentiate the different between structure and union
Structure
▪ In C++, a structure is a user-defined data type that allows you to
group different variables of different data types together. It is
similar to a class, but with some key differences.

▪ The syntax for defining a structure in C++ is as follows:

struct MyStructure
{
// Variables (also known as members or fields) declared here
type1 variable1;
type2 variable2;
// ...
};
Explanation
▪ MyStructure is the name of the structure, and type1, type2, and so
on, represent the data types of the variables within the structure.

▪ Once you have defined a structure, you can create variables of


that structure type, just like any other data type:

MyStructure myVariable;

▪ You can then access the variables within the structure using the
dot operator (.) as follows:

myVariable.variable1 = value1;
myVariable.variable2 = value2;
Structure
▪ Structures are commonly used to group related variables together
to represent a concept or entity.

▪ For example, you could define a structure to represent a person


with variables such as name, age, and address. By grouping these
variables into a structure, you can easily manipulate and pass
them as a single entity.
#include <iostream>
using namespace std;
Example
// Define the structure
struct Person {
string name;
int age;
string address;
};

int main() {
// Create a variable of type Person
Person person;

// Access and assign values to the variables within the structure


person.name = "Abu Bakar";
person.age = 35;
person.address = "Cheras, Alam Damai";
// Display the information stored in the structure
cout << "Name: " << person.name << endl;
cout << "Age: " << person.age << endl;
cout << "Address: " << person.address << endl;

return 0;
}
Explanation
▪ In this program, we define a structure called Person with three
member variables: name, age, and address. Inside the main
function, we create a variable named person of type Person.

▪ We then assign values to its member variables using the dot operator
(.).

▪ Finally, we print the values stored in the structure using cout.

Output: Name: Abu Bakar


Age: 35
Address: Cheras, Alam Damai

▪ This example demonstrates how a structure can be used to group


related data together and access it using the structure variable and
the dot operator.
Advantages of Structure

▪ Structures gather more than one piece of data about the same
subject together in the same place.

▪ It is helpful when you want to gather the data of similar data types
and parameters like first name, last name, etc.

▪ It is very easy to maintain as we can represent the whole record


by using a single name.

▪ In structure, we can pass complete set of records to any function


using a single parameter.

▪ You can use an array of structure to store more records with similar
types.
Unions
▪ In C++, a union is a user-defined data type that allows different
variables to share the same memory space.

▪ Unlike structures, where each member variable has its own


separate memory location, all variables within a union occupy the
same memory location.

▪ The syntax for defining a union in C++ is as follows:


union MyUnion
{
// Member variables declared here
type1 variable1;
type2 variable2;
// ...
};
Explanation
▪ MyUnion is the name of the union, and type1, type2, and so on,
represent the data types of the variables within the union.

▪ When you create a union variable, memory is allocated to hold


the largest member variable of the union.

▪ The size of the union is determined by the size of its largest


member. All member variables within the union share this same
memory space.

▪ You can access the member variables of a union using the dot
operator (.), similar to how you access structure members:
Explanation MyUnion myVariable;
myVariable.variable1 = value1;
▪ It is important to note that only one member variable of a union
can be active at a time. Assigning a value to one member
variable will overwrite the value of any other member variable
that was previously set.

▪ Unions are commonly used in situations where you want to store


different types of data in the same memory location and access
them interchangeably. They can be helpful in optimizing memory
usage or when you need to work with different data types based
on certain conditions.

▪ It is crucial to use unions with caution, as accessing the wrong


member variable can lead to unexpected behavior or incorrect
data interpretation.
#include <iostream>
Example using namespace std;

union MyUnion
{
int intValue;
float floatValue;
char charValue;
};

int main()
{
MyUnion myVariable;

myVariable.intValue = 42;
cout << "Integer value: " << myVariable.intValue << endl;

myVariable.floatValue = 3.14;
cout << "Float value: " << myVariable.floatValue << endl;

myVariable.charValue = 'A';
cout << "Character value: " << myVariable.charValue << endl;

cout << "Integer value after assigning character: " << myVariable.intValue << endl;

return 0;
}
Output
Integer value: 42
Float value: 3.14
Character value: A
Integer value after assigning character: 65
Difference between Structures & Union
Key Differences

▪ Every member within structure is assigned a unique memory


location while in union a memory location is shared by all the
data members.
▪ Changing the value of one data member will not affect other
data members in structure whereas changing the value of one
data member will change the value of other data members in
union.
▪ Structure is mainly used for storing various data types while union
is mainly used for storing one of the many data types.
▪ In structure, you can retrieve any member at a time on the other
hand in union, you can access one member at a time.
▪ Structure supports flexible array while union does not support a
flexible array.
*** The End ***

You might also like