Ticker

6/recent/ticker-posts

Structures and Unions In C

 Chapter
14: Structures and Unions



 




In this chapter, we will explore structures and
unions in C. Structures allow you to create custom data types that group
related data together, while unions provide a way to allocate a single block of
memory that can be accessed as different types. We will cover creating and
using structures, nested structures, unions, and the differences between
structures and unions. Let's get started!



 



14.1
Creating and Using Structures:



A structure in C is a user-defined data type that
allows you to combine different data types into a single entity. Each member
within a structure can have a different data type.



 



Example:





#include <stdio.h>



 



// Define a structure



struct Point {



   
int x;



   
int y;



};



 



int main() {



   
// Declare a structure variable



   
struct Point p1;



 



   
// Access and assign values to the structure members



   
p1.x = 10;



   
p1.y = 20;



 



   
// Print the values



   
printf("Coordinates: (%d, %d)\n", p1.x, p1.y);



 



   
return 0;



}



 





In
this example, we define a structure `Point` with two members `x` and `y`. We
declare a structure variable `p1` of type `Point` and assign values to its
members using the dot operator (`.`). Finally, we print the values of `x` and
`y`.



 



14.2
Nested Structures:



C allows you to nest structures within other
structures, providing a way to represent more complex data structures.



 



Example:





#include
<stdio.h>



// Define a
structure



struct Date {



    int day;



    int month;



    int year;



};



 



// Define a
structure with nested structures



struct Employee
{



    int id;



    char name[20];



    struct Date birthdate;



};



 



int main() {



    // Declare a structure variable



    struct Employee emp;



 



    // Access and assign values to the nested
structure members



    emp.id = 1;



    strcpy(emp.name, "John Doe");



    emp.birthdate.day = 15;



    emp.birthdate.month = 5;



    emp.birthdate.year = 1990;



 



    // Print the values



    printf("Employee ID: %d\n",
emp.id);



    printf("Employee Name: %s\n",
emp.name);



    printf("Birthdate: %d/%d/%d\n",
emp.birthdate.day, emp.birthdate.month, emp.birthdate.year);



 



    return 0;



}





 



In this example, we define a structure `Date` to
represent a date and another structure `Employee` that contains the employee's
ID, name, and birthdate (nested structure). We declare a structure variable
`emp` of type `Employee` and assign values to its members, including the nested
structure `birthdate`. Finally, we print the values of the employee's ID, name,
and birthdate.



 



14.3
Unions and their Differences with Structures:



Unions in C are similar to structures but allocate a
single block of memory that can be accessed using different member names. The
memory allocated for a union is equal to the size of its largest member.



 



Example:





#include
<stdio.h>



 



//
Define a union



union
Data {



    int intValue;



    float floatValue;



    char stringValue[20];



};



 



int
main() {



    // Declare a union variable



    union Data data;







 



   
// Assign values to the union members



   
data.intValue = 10;



   
printf("Integer value: %d\n", data.intValue);



 



   
data.floatValue = 3.14;



   
printf("Float value: %.2f\n", data.floatValue);



 



   
strcpy(data.stringValue, "Hello");



   
printf("String



 



 value: %s\n", data.stringValue);



 



   
return 0;



}





 



 



In this example, we define a union `Data` with three
members: `intValue` (integer), `floatValue` (float), and `stringValue`
(character array). We declare a union variable `data` and assign values to its
members. Notice that each time we assign a value, the previous value gets
overwritten because the union shares the same memory space. Finally, we print
the values of the members.



 



The main difference between structures and unions is
that structures allocate memory for each member independently, while unions
allocate memory for the largest member only. This makes unions useful when you
need to represent different data types within the same memory block.



 



14.4
Typedef and Structures:



The `typedef` keyword can be used with structures to
create aliases or alternative names for structure types, making the code more
readable and manageable.



 



Example:





#include <stdio.h>



 



// Define a structure



typedef struct {



   
int x;



   
int y;



} Point;



 



int main() {



   
// Declare a structure variable



   
Point p1;



 



   
// Access and assign values to the structure members



   
p1.x = 10;



   
p1.y = 20;



 



   
// Print the values



   
printf("Coordinates: (%d, %d)\n", p1.x, p1.y);



 



   
return 0;



}





 



 



In this example, we use `typedef` to create an alias
`Point` for the structure type. We then declare a structure variable `p1` of
type `Point` and assign values to its members using the dot operator (`.`).
Finally, we print the values of `x` and `y`.



 



That concludes our exploration of structures and
unions in C. Structures allow you to group related data together, while unions
provide a way to access the same memory block as different types. Nested
structures can be used to represent more complex data structures. The `typedef`
keyword can be used to create aliases for structure types, improving code
readability.



 



Continue practicing and experimenting with
structures and unions to strengthen your understanding. In the next chapter, we
will cover input and output operations in C.

Post a Comment

0 Comments