0% found this document useful (0 votes)
11 views4 pages

C Unit 4 notes

Uploaded by

shrushti0221
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
11 views4 pages

C Unit 4 notes

Uploaded by

shrushti0221
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

UNIT 4 Structure & Union

Module 1: Introduction to Structure

1. Definition of Structure

A structure in C is a user-defined data type that allows the


combination of variables of different data types under a single name.
Structures are used to group related data together, making
the program more organized and easier to understand.

Each variable in a structure is called a member or field. A structure


is particularly useful when you want to represent a record with
multiple attributes.

2. Declaration of a Structure

The structure is declared using the struct keyword, followed by


the structure's name and its members enclosed in curly braces
{}.

Syntax:
struct StructureName {
dataType member1;
dataType member2;
// additional members
};

Example:
struct Person {
char name[50];
int age;
float height;
};

Here, Person is a structure with three members: name, age, and height.

3. Accessing Structure Members

To access the members of a structure, you use the dot


operator (.) if you have a structure variable.

Syntax:
structureVariable.memberName

Example:
struct Person p1;
strcpy(p1.name, "Alice"); // Accessing the 'name' member of 'p1'
p1.age = 25; // Accessing the 'age' member of 'p1'
p1.height = 5.6; // Accessing the 'height' member of 'p1'
For accessing structure members via pointers, you
use the arrow operator (->). Example:
struct Person *ptr = &p1;
printf("Name: %s\n", ptr->name); // Using pointer
to access structure member 4. Structure
Initialization
Structures can be initialized at the time of declaration, just like arrays.

Example:
struct Person p1 = {"Alice", 25, 5.6};

You can also assign values to structure members after declaration:


struct Person p1;
p1.age = 25;
strcpy(p1.name, "Alice");
p1.height = 5.6;

5. Structure Operations

∙Comparison: You cannot compare two structure variables


directly using comparison operators like == or !=. You need
to compare each member individually. ∙ Assignment: You can
assign one structure variable to another (both having the
same type).

Example:
struct Person p1 = {"Alice", 25, 5.6};
struct Person p2;
p2 = p1; // Copying all members of p1 to p2

6. Nested Structures

A structure can contain another structure as one of its


members. This is known as nested structures.

Example:
struct Address {
char city[50];
char state[50];
};

struct Person {
char name[50];
int age;
struct Address address; // Nested structure
};

Accessing members of nested structures:


struct Person p1;
strcpy(p1.name, "Alice");
p1.age = 25;
strcpy(p1.address.city, "New York");
strcpy(p1.address.state, "NY");
Module 2: Introduction to Union

1. Definition of Union

A union in C is similar to a structure, but it allows multiple members


to share the same memory location. This means that all members of a
union occupy the same memory space, and at any given time, only
one member can hold a value. A union is used when you need to store
different types of data, but not necessarily at the same time.

2. Declaration of a Union

The syntax for declaring a union is very similar to that of a structure.


You use the union keyword instead of struct.

Syntax:
union UnionName {
dataType member1;
dataType member2;
// additional members
};

Example:
union Data {
int i;
float f;
char c;
};

In this example, Data is a union that can store an integer, a float, or a


character, but only one of them at a time.

3. Accessing Union Members

Union members are accessed using the dot operator (.) just like
structure members.

Example:
union Data data;
data.i = 10; // Assigning an integer to the union
printf("%d\n", data.i); // Output: 10

data.f = 220.5; // Assigning a float to the union


printf("%f\n", data.f); // Output: 220.5

Here, the value of data.i will be overwritten by the value of data.f,


because they share the same memory.
4. Union Initialization

A union can be initialized when it is declared, just like structures.

Example:
union Data data = {10}; // Initializes the first member, 'i', of the union
5. Size of a Union

The size of a union is determined by the size of its largest member


because all members share the same memory. The size of a structure
is the sum of the sizes of its members.

Example:
union Data data;
printf("Size of union: %lu\n", sizeof(data)); // Output: Size of
union: 4 (assuming the largest member is an integer)

Difference Between Structure and Union


Feature Structure Union
All members share the
Memory Each member gets its own same memory
Allocation memory location. The total location. The total size
size of the structure is the sum is the size of the
Member
of the sizes of its members. largest member. Only
Storage
one member can hold a
All members can hold value at any time.
Size
values at the same time.
The size of the union
Accessing is the size of the
Members The size of the structure is
largest member.
the sum of the sizes of all
its members. Only one member can
Use Case
be accessed at a
Each member is accessed time.
Example independently. Used when you need
to store different
Used when you need to types of data, but not
store related but different at the same time.
types of data that are
used simultaneously. Unions like union
Data to store int,
Structures like struct Person, float, or char.
struct Date

You might also like