0% found this document useful (0 votes)
8 views

Structure and Union Upto Sorting PDF - 240817 - 115443

Structure and union upto sorting pdf

Uploaded by

opwumpus
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)
8 views

Structure and Union Upto Sorting PDF - 240817 - 115443

Structure and union upto sorting pdf

Uploaded by

opwumpus
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/ 26

Structure and Union

Structure
• Structure stores the different types of elements i.e heterogeneous
elements. The struct keyword is used to define structure.

• The structure is the container defined in C to store data variables of


different types and also supports the user-defined variable’s storage.
struct structure_name

Syntax
struct structure_name
{
data_type member1;
.
.
data_type memberN;
};
Declaring structure variable

1. By struct keyword within main() function


2. By declaring variable at the time of defining structure.
Example(Method 1)
#include <stdio.h> {
#include <string.h> struct student s1;
struct student s1.rollno=1;
{ strcpy(s1.name, "Saroj");
int rollno; printf( "Rollno : %d\n", s1.rollno);
char name[60]; printf( "Name : %s\n", s1.name);
}; return 0;
int main( ) }
Example(Method 2)
#include <stdio.h> {
#include <string.h> s1.rollno=1;
struct student strcpy(s1.name, "Saroj");
{ printf( "Rollno : %d\n", s1.rollno);
int rollno; printf( "Name : %s\n", s1.name);
char name[60]; return 0;
}s1; }
int main( )
Example of Storing details of two students using structure

#include <stdio.h> s2.rollno=2;


#include <string.h> strcpy(s2.name,"Manoj");
struct student strcpy(s1.name, "Saroj");
{ printf( "Rollno : %d\n", s1.rollno);
int rollno; printf( "Name : %s\n\n", s1.name);
char name[60]; printf( "Rollno : %d\n", s2.rollno);
}s1,s2; printf( "Name : %s\n", s2.name);
int main( ) return 0;
{ }
s1.rollno=1;
Program to print the details of N
numbers students using
structure
#include <stdio.h> printf("Name: ");
#include <string.h> scanf("%s", &s_details[i].name);

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.

Unlike structures, which allocate memory for each member


separately, a union allocates enough memory to hold the
largest member and allows you to access any member of the
union using a single memory location.
syntax
union union_name
{
data_type member1;
.
.
data_type memberN;
};
Declaring union variable

1. By union keyword within main() function


2. By declaring variable at the time of defining union.
Example(Method 1)
#include <stdio.h> {
#include <string.h> union student s1;
union student s1.rollno=1;
{ strcpy(s1.name, "Saroj");
int rollno; printf( "Rollno : %d\n", s1.rollno);
char name[60]; printf( "Name : %s\n", s1.name);
}; return 0;
int main( ) }`
Example(Method 2)
#include <stdio.h> {
#include <string.h> s1.rollno=1;
union student strcpy(s1.name, "Saroj");
{ printf( "Rollno : %d\n", s1.rollno);
int rollno; printf( "Name : %s\n", s1.name);
char name[60]; return 0;
}s1; }
int main( )
Example of Storing details of two students using union

#include <stdio.h> s2.rollno=2;


#include <string.h> strcpy(s2.name,"Manoj");
union student strcpy(s1.name, "Saroj");
{ printf( "Rollno : %d\n", s1.rollno);
int rollno; printf( "Name : %s\n\n", s1.name);
char name[60]; printf( "Rollno : %d\n", s2.rollno);
}s1,s2; printf( "Name : %s\n", s2.name);
int main( ) return 0;
{ }
s1.rollno=1;
Program to print the details of N
numbers students using
union
#include <stdio.h> struct Student students[n]; printf("\nStudent %d:\n", i + 1);
printf("Name: %s\n", students[i].details.name);
// Union to hold different data types for student details for (int i = 0; i < n; i++) { printf("Roll Number: %d\n",
students[i].details.roll_number);
union StudentDetails { printf("\nEnter details for student %d:\n", i + 1);
printf("Marks: %.2f\n", students[i].details.marks);
char name[50];
}
int roll_number; // Input student name
float marks; printf("Enter name: ");
return 0;
}; scanf(" %[^\n]", students[i].details.name);
}

// 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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy