Unit 4 Part 1
Unit 4 Part 1
INTRODUCTION TO PROGRAMMING
UNIT – 4 PART – 1
Structures and Unions:
What is a Structure in C?
Features of a Structure
Syntax of a Structure
struct StructureName {
data_type member1; // Member 1 declaration
data_type member2; // Member 2 declaration
// Add more members as needed
};
Example Syntax
struct Employee {
int id; // Integer variable to store employee ID
char name[50]; // Character array to store employee name
float salary; // Float variable to store employee salary
};
Once a structure is defined, you can declare variables of that structure type.
There are two ways:
struct Employee {
int id;
char name[50];
float salary;
} emp1, emp2;
Direct Initialization
Here:
id is initialized to 101.
name is initialized to "Alice".
salary is initialized to 75000.0.
Partial Initialization
id is initialized to 102.
name is initialized to "Bob".
salary is initialized to 0.0 (default value for float).
To access or modify the members of a structure, use the dot operator (.) with
the structure variable.
Example:
#include <stdio.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
struct Employee emp1; // Declare a structure variable
return 0;
}
Output:
Employee Details:
ID: 101
Name: John Doe
Salary: 75000.50
Memory Allocation: Each member has its own memory location, and
the size of the structure is the sum of all its members (plus potential
padding for alignment).
Initialization: Structure members can be initialized at the time of
declaration:
K D V PAVAN KUMAR VARMA, DEPT OF CSE, SRKREC
Advantages of Structures
Real-World Applications
Nested Structures in C
data_type outerMember;
};
Explanation
// Nested Structures
K D V PAVAN KUMAR VARMA, DEPT OF CSE, SRKREC
struct Address {
char city[50];
int zipCode;
};
struct Employee {
int id;
char name[50];
struct Address address; // Nested structure as a member
};
int main() {
struct Employee emp = {101, "John Doe", {"New York", 10001}};
return 0;
}
Output:
Employee Details:
ID: 101
Name: John Doe
City: New York
ZIP Code: 10001
1. Use the dot operator (.) with the outer structure variable for direct
access.
2. If using a pointer to a structure, use the arrow operator (->).
#include <stdio.h>
// Nested Structures
struct Marks {
int math;
int science;
int english;
};
K D V PAVAN KUMAR VARMA, DEPT OF CSE, SRKREC
struct Student {
int rollNumber;
char name[50];
struct Marks marks; // Nested structure as a member
};
int main() {
struct Student student1 = {1, "Bob", {85, 90, 78}};
printf("Student Details:\n");
printf("Roll Number: %d\n", student1.rollNumber);
printf("Name: %s\n", student1.name);
printf("Marks:\n");
printf(" Math: %d\n", student1.marks.math);
printf(" Science: %d\n", student1.marks.science);
printf(" English: %d\n", student1.marks.english);
return 0;
}
Output:
Student Details:
Roll Number: 1
Name: Bob
Marks:
Math: 85
Science: 90
English: 78
Array of Structures
Key Points
Syntax
struct StructureName {
data_type member1;
data_type member2;
// Additional members
};
K D V PAVAN KUMAR VARMA, DEPT OF CSE, SRKREC
// Define a structure
struct Employee {
int id; // Employee ID
char name[50]; // Employee name
float salary; // Employee salary
};
int main() {
// Declare and initialize an array of structures
struct Employee emp[3] = {
{101, "John", 50000.0}, // First employee
{102, "Alice", 60000.0}, // Second employee
{103, "Bob", 55000.0} // Third employee
};
Output
Employee 1:
ID: 101
Name: John
Salary: 50000.00
Employee 2:
ID: 102
Name: Alice
Salary: 60000.00
Employee 3:
ID: 103
Name: Bob
Salary: 55000.00
Pointer to Structure
Key Points
Syntax
struct StructureName {
data_type member1;
data_type member2;
// Additional members
};
// Define a structure
struct Employee {
int id; // Employee ID
char name[50]; // Employee name
float salary; // Employee salary
};
int main() {
struct Employee emp = {101, "John", 50000.0}; // Initialize a structure variable
return 0;
}
K D V PAVAN KUMAR VARMA, DEPT OF CSE, SRKREC
Output
Employee Details:
ID: 101
Name: John
Salary: 50000.00
struct student {
int roll;
char name[50];
float percentage;
};
int main() {
// Array of structures
struct student students[3] = {
{1, "Alice", 85.5},
{2, "Bob", 90.2},
{3, "Charlie", 78.0}
};
// Pointer to structure
struct student *ptr = students; // Pointer points to the first element of the array
return 0;
}
Explanation:
1. Structure Array:
o students[3] is an array of struct student, containing information for 3
students.
2. Pointer to Structure Array:
o The pointer ptr points to the first element of the students array (ptr =
students).
3. Pointer Arithmetic:
K D V PAVAN KUMAR VARMA, DEPT OF CSE, SRKREC
Output:
Student 1:
Roll: 1
Name: Alice
Percentage: 85.50
Student 2:
Roll: 2
Name: Bob
Percentage: 90.20
Student 3:
Roll: 3
Name: Charlie
Percentage: 78.00
Unions in C
Syntax
union UnionName {
data_type member1;
data_type member2;
// Additional members
};
Example
#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
return 0;
}
Output
Integer: 10
Float: 220.5
String: C Programming
Integer after string assignment: 1129270631
K D V PAVAN KUMAR VARMA, DEPT OF CSE, SRKREC
Explanation
#include <stdio.h>
struct StructureExample {
int i;
float f;
char str[20];
};
union UnionExample {
int i;
float f;
char str[20];
};
int main() {
printf("Size of Structure: %lu bytes\n", sizeof(struct StructureExample));
printf("Size of Union: %lu bytes\n", sizeof(union UnionExample));
return 0;
}
Output
Bit Fields
Syntax
struct BitFieldName {
unsigned int member1 : bit_count; // Allocate 'bit_count' bits to member1
unsigned int member2 : bit_count; // Allocate 'bit_count' bits to member2
};
Example
#include <stdio.h>
int main() {
struct BitField bit;
// Example of overflow
bit.a = 10; // Binary: 1010 (truncated to 3 bits)
printf("After assigning 10 to 'a': %u\n", bit.a);
return 0;
}
Output
a: 5, b: 3
After assigning 10 to 'a': 2
Explanation
Programs
#include <stdio.h>
int main() {
int n, i;
float total = 0, average;
// Calculate average
average = total / n;
return 0;
}
// Define a structure
struct MyStruct {
int intVal;
float floatVal;
char charVal;
};
// Define a union
union MyUnion {
int intVal;
float floatVal;
char charVal;
};
int main() {
struct MyStruct s = {1, 2.5, 'A'};
union MyUnion u;
return 0;
}
// Define a structure
struct Student {
char name[50];
int age;
float marks;
};
int main() {
// Initialize a structure
struct Student student1 = {"Alice", 20, 85.5};
// Copy structure
struct Student student2 = student1;
return 0;
}