Structure and Union Upto Sorting PDF - 240817 - 115443
Structure and Union Upto Sorting PDF - 240817 - 115443
Structure
• Structure stores the different types of elements i.e heterogeneous
elements. The struct keyword is used to define structure.
Syntax
struct structure_name
{
data_type member1;
.
.
data_type memberN;
};
Declaring structure variable
printf("Age: ");
// Define the structure to store student details scanf("%d", &s_details[i].age);
struct Student {
char name[30]; printf("GPA: ");
int age; scanf("%f", &s_details[i].gpa);
float gpa; }
}s_details[100];
// Display student details
int main() { printf("\nStudent Details:\n");
int n; // Number of students for (int i = 0; i < n; i++) {
printf("\nRoll.No %d:\n", i + 1);
printf("Enter the number of students\n"); printf("Name: %s\n", s_details[i].name);
scanf("%d", &n); printf("Age: %d\n", s_details[i].age);
printf("GPA: %.2f\n", s_details[i].gpa);
// Input student details }
for (int i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1); return 0;
}
Union
A union is a user-defined data type that allows us to store
different data types in the same memory location.
// Structure to represent a student record using the // Input student roll number
union
printf("Enter roll number: ");
struct Student {
scanf("%d", &students[i].details.roll_number);
union StudentDetails details;
};
// Input student marks
printf("Enter marks: ");
int main() {
scanf("%f", &students[i].details.marks);
int n;
}
printf("Enter the number of students: ");
scanf("%d", &n);
// Print the details of all the students
printf("\nDetails of %d students:\n", n);
// Create an array of struct Student to store details of
multiple students for (int i = 0; i < n; i++) {
Differentiate Between Structure and Union
Structure Union
1.To declare a Structure the keyword 'struct' is used. 1.To declare a Union the keyword 'union’ is used.
2. The compiler allocates the memory for each 2. The compiler allocates memory by considering
member when a variable is associated with a the size of the largest member when a variable is
structure. The size of a structure is greater or equal associated with union. The size of the union is equal
to the sum of the sizes of its members. to the size of the largest Member.
3. Each member within a Structure is assigned a 3. All the members of the union share the same
unique storage area of location. memory allocated.
4. The address of each member in the structure is in 4. The address is the same for all the members of a
ascending order, that is, memory for each member union, that is, every member begins at the same
starts at different offset values. offset value.
5. Manipulation of one member of structure won't 5. Manipulation of one member will affect the other
affect the values of any other member members' value in case of union.
6. We can initialize multiple variables at a time. 6. Only the first data member can be initialized.
7. A structure allows accessing and retrieving any 7. A union allows retrieving only one data member
data member at a time. at a time.
Nested Structure
Introduction:
A nested structure in C is a structure that contains one or more
members that are themselves structures. Nested structures are useful
for organizing complex data and can help improve a program's
readability and maintainability.
Syntax Of Nested Structure
struct outer_structure { struct employee {
type member1; int id;
type member2; char name[50];
struct inner_structure { struct address {
type inner_member1; char street[50];
type inner_member2; char city[50];
} inner; char state[50];
}; char zip[10];
} addr;
};
Example of Nested
Structure
{ printf("Employee id : %d\n",
#include <stdio.h> struct Organisation org; org.emp.employee_id);
#include <string.h> printf("Employee name : %s\n",
printf("The size of structure organisation : org.emp.name);
%ld\n", sizeof(org));
struct Employee printf("Employee Salary : %d\n",
{ org.emp.salary);
org.emp.employee_id = 101;
int employee_id; }
strcpy(org.emp.name, "SAROJ
char name[20];
POUDEL");
int salary;
org.emp.salary = 900000;
};
strcpy(org.organisation_name,
struct Organisation
"KMC");
{
strcpy(org.org_number, "KMC428");
char organisation_name[20];
char org_number[20];
struct Employee emp;
printf("Organisation Name : %s\n",
};
org.organisation_name);
printf("Organisation Number : %s\n",
int main()
org.org_number);
Another Example of Nested
Structure
{ %s\n",
#include <stdio.h> org.organisation_name);
#include <string.h> printf("The size of structure printf("Organisation Number :
organisation : %ld\n", %s\n",
sizeof(org)); org.org_number);
struct Organisation
{ printf("Employee id : %d\n",
org.emp.employee_id = 101; org.emp.employee_id);
char organisation_name[20]; strcpy(org.emp.name, "SAROJ
char org_number[20]; POUDEL"); printf("Employee name :
%s\n",
struct Employee org.emp.salary = 900000; org.emp.name);
{ printf("Employee Salary :
int employee_id; strcpy(org.organisation_name, %d\n",
char name[20]; "KMC"); org.emp.salary);
int salary; strcpy(org.org_number, }
"KMC428");
}emp;
}org;
printf("Organisation Name :
int main()
SORTING EXAMPLE OF
STRUCTURE
#include <stdio.h> for (i = 0; i < n; i++) {
#include <string.h> {
struct Student printf("Student %d:\n", i + 1); temp = s[i];
{ printf("Roll Number: "); s[i] = s[j];
int rN; scanf("%d", &s[i].rN); s[j] = temp;
char fN[30]; printf("First Name: "); }
char lN[30]; scanf(" %s", s[i].fN); }
char a[100]; printf("Last Name: "); }
scanf(" %s", s[i].lN); printf("\nStudent Records in
Descending Order (Based on Marks in
float mC; printf("Address: "); Computer):\n");
}; scanf(" %[^\n]s", s[i].a); for (i = 0; i < n; i++)
int main() printf("Marks in Computer: "); {
{ scanf("%f", &s[i].mC); printf("Roll Number: %d, Name: %s
int n, i, j; } %s, Address: %s, Marks in Computer:
%.2f\n", s[i].rN, s[i].fN, s[i].lN, s[i].a,
printf("Enter the number of students: for (i = 0; i < n - 1; i++) s[i].mC);
");
{ }
scanf("%d", &n); for (j = i + 1; j < n; j++) return 0;
struct Student s[n], temp;
{ }
printf("Enter student details:\n");
if (s[i].mC < s[j].mC)