0% found this document useful (0 votes)
9 views10 pages

Use of Structure and Union in - C - 29-May-2024 - SRC

The document explains structures and unions in C programming, highlighting that structures group different data types under a single name while unions share the same memory location for their members. It provides examples of both structures and unions, demonstrating their usage and the benefits of each, such as data organization and efficient memory usage. Additionally, it discusses nesting of structures and unions, allowing for the creation of complex data types.

Uploaded by

debolinad3y
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)
9 views10 pages

Use of Structure and Union in - C - 29-May-2024 - SRC

The document explains structures and unions in C programming, highlighting that structures group different data types under a single name while unions share the same memory location for their members. It provides examples of both structures and unions, demonstrating their usage and the benefits of each, such as data organization and efficient memory usage. Additionally, it discusses nesting of structures and unions, allowing for the creation of complex data types.

Uploaded by

debolinad3y
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/ 10

Structures and Unions in C src7bppimt@gmail.

com 29-May-2024 Page 1 of 10

Structures and Unions in C


*Disclaimer: *help is taken from Chatgpt 3.5 and Google Gemini, but finally tested on Linux machine, gcc compiler

C provides two powerful tools for organizing data: structures and unions.

Structures:

●​ A structure is a user-defined data type that groups variables of different data types
under a single name. These variables are called members of the structure.
●​ Structures allow you to create complex data objects that represent real-world entities
or concepts.

Structures and Unions in C [email protected] 29-May-2024 Page 1 of 10


Structures and Unions in C [email protected] 29-May-2024 Page 2 of 10

#include <stdio.h>

// Structure declaration
struct Person {
char name[50];
int age;
float height;
};

int main() {
// Structure variable
struct Person person1;

// Assigning values to the structure members


strcpy(person1.name, "Alice");
person1.age = 30;
person1.height = 5.6;

// Accessing structure members


printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.1f\n", person1.height);

return 0;
}

Structures and Unions in C [email protected] 29-May-2024 Page 2 of 10


Structures and Unions in C [email protected] 29-May-2024 Page 3 of 10

Unions:
●​ A union is similar to a structure, but all members share the same memory location.
This means only one member can have a value at any given time.
●​ Unions are useful for situations where you need to store different data types that
don't necessarily need to be used simultaneously, saving memory space.

Structures and Unions in C [email protected] 29-May-2024 Page 3 of 10


Structures and Unions in C [email protected] 29-May-2024 Page 4 of 10

Why Use Structures and Unions?

Structures:

●​ Data Organization: Structures help organize related data into a single unit, making
your code more readable and maintainable.
●​ Creating Complex Objects: You can create structures that represent real-world
entities like Point (x and y coordinates), Student (name, ID, grade), etc.

Unions:

●​ Efficient Memory Usage: Unions are space-efficient when you know only one
member will be used at a time.
●​ Variant Data Types: You can store different data types in a union, depending on the
program's needs.

Structures and Unions in C [email protected] 29-May-2024 Page 4 of 10


Structures and Unions in C [email protected] 29-May-2024 Page 5 of 10

Q: What is a union in C?

A: A union in C is a user-defined data type that allows storing different data types in the
same memory location.

Unlike a structure, a union can hold only one of its members at a time, saving memory by
sharing the same space for all members.

Q: How do you declare and define a union?

A: You declare a union using the union keyword followed by the union name and its
members.

Structures and Unions in C [email protected] 29-May-2024 Page 5 of 10


Structures and Unions in C [email protected] 29-May-2024 Page 6 of 10

Here is an example of union :

#include <stdio.h>

// Union declaration
union Data {
int i;
float f;
char str[20];
};

int main() {
// Union variable
union Data data;

// Assigning and accessing union members one at a time


data.i = 10;
printf("data.i: %d\n", data.i);

data.f = 220.5;
printf("data.f: %.1f\n", data.f);

strcpy(data.str, "Hello");
printf("data.str: %s\n", data.str);

return 0;
}

Q: When would you use a structure?

A: You would use a structure when you need to group different types of data together and
access all the members independently. Structures are ideal for representing objects with
multiple attributes, such as a record in a database.

Q: When would you use a union?

A: You would use a union when you need to store different types of data in the same memory
location, but only one type at a time. Unions are useful in situations where you need to save
memory, such as in embedded systems or when dealing with multiple representations of data.

Structures and Unions in C [email protected] 29-May-2024 Page 6 of 10


Structures and Unions in C [email protected] 29-May-2024 Page 7 of 10

Q: Can you access all members of a union simultaneously?

A: No, you cannot access all members of a union simultaneously because they share the same
memory location. Accessing one member will overwrite the other members.

Q: Can you access all members of a structure simultaneously?

A: Yes, you can access all members of a structure simultaneously because each member has its
own memory space.

Q: Can structures and unions be nested?

A: Yes, structures and unions can be nested within each other. You can have structures inside
unions, unions inside structures, and even structures or unions as members of other structures
or unions. This allows for the creation of complex data types.

Structures and Unions in C [email protected] 29-May-2024 Page 7 of 10


Structures and Unions in C [email protected] 29-May-2024 Page 8 of 10

Example of Nested Structures and Unions

#include <stdio.h>

// Nested structure and union


struct Address {
char street[50];
int zip;
};

struct Person {
char name[50];
int age;
union {
struct Address address;
char phone[15];
} contact;
};

int main() {
struct Person person1;

// Assigning values to the structure members


strcpy(person1.name, "Bob");
person1.age = 45;
strcpy(person1.contact.address.street, "123 Main St");
person1.contact.address.zip = 12345;

// Accessing nested structure members


printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Street: %s\n", person1.contact.address.street);
printf("ZIP: %d\n", person1.contact.address.zip);

// Using union member


strcpy(person1.contact.phone, "555-1234");
printf("Phone: %s\n", person1.contact.phone);

return 0;
}

In this example, Person contains a nested structure Address inside a union contact,
demonstrating how complex data types can be created by combining structures and unions.

Structures and Unions in C [email protected] 29-May-2024 Page 8 of 10


Structures and Unions in C [email protected] 29-May-2024 Page 9 of 10

Remember:

●​ Structures group related data of different types.


●​ Unions share the same memory location for members, allowing for different
data types, but only one value can be active.
●​ Use structures for data organization and representing real-world entities.
●​ Use unions for efficient memory usage when only one member will be used
at a time.

*** END ***

Structures and Unions in C [email protected] 29-May-2024 Page 9 of 10


Structures and Unions in C [email protected] 29-May-2024 Page 10 of 10

Structures and Unions in C [email protected] 29-May-2024 Page 10 of 10

You might also like