0% found this document useful (0 votes)
2 views17 pages

Structures

Uploaded by

kgautam21080410
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Structures

Uploaded by

kgautam21080410
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

What is Member-wise Copy?

When you assign one structure variable to another of the same type, C
performs a member-wise copy.

That means:

 Each member (field) is copied individually

 Values are duplicated, not shared

 Both structures are stored in different memory locations

Key Points

 ✔ Structures must be of the same type

 ✔ All members are copied one by one

 ✔ It is a deep copy (for basic data types)

 ✔ Changing one structure does NOT affect the other

Example 1: Simple Member-wise Copy

#include <stdio.h>
struct Student {
int id;
float marks;
};
int main() {
struct Student s1 = {101, 85.5};
struct Student s2;
// Member-wise copy
s2 = s1;

printf("s1: ID = %d, Marks = %.2f\n", [Link], [Link]);


printf("s2: ID = %d, Marks = %.2f\n", [Link], [Link]);
return 0;
}

Output

s1: ID = 101, Marks = 85.50


s2: ID = 101, Marks = 85.50

Example 2: Proving They Are Independent

#include <stdio.h>
struct Student {
int id;
float marks;
};
int main() {
struct Student s1 = {101, 85.5};
struct Student s2;
s2 = s1;
// Change s2
[Link] = 200;
[Link] = 90.0;
printf("After modification:\n");
printf("s1: ID = %d, Marks = %.2f\n", [Link], [Link]);
printf("s2: ID = %d, Marks = %.2f\n", [Link], [Link]);
return 0;
}

Output

After modification:
s1: ID = 101, Marks = 85.50
s2: ID = 200, Marks = 90.00

Notice:

 Changing s2 does NOT affect s1

 This proves separate memory locations

Example 3: Structure with Array Member

#include <stdio.h>
#include <string.h>
struct Employee {
int id;
char name[20];
};
int main() {
struct Employee e1 = {1, "Vidhya"};
struct Employee e2;

e2 = e1;

printf("e1: %d %s\n", [Link], [Link]);


printf("e2: %d %s\n", [Link], [Link]);

return 0;
}

Output

e1: 1 Vidhya
e2: 1 Vidhya

Even arrays inside structures are copied element by element


What is typedef?

typedef is used to give a new (alias) name to an existing data type.

Important:

 It does NOT create a new data type

 It only provides a new name (alias)

Syntax

typedef existing_datatype new_name;

Basic Example

#include <stdio.h>
typedef int integer;
int main() {
integer a = 10, b = 20;
printf("a = %d, b = %d", a, b);
return 0;
}

Output

a = 10, b = 20

Why use typedef with Structures?


Normally in C, we write:

struct Student s1;

This is long and repetitive.

Using typedef, we can simplify it:

Student s1;

Example 1: Structure WITHOUT typedef

#include <stdio.h>
struct Student {
int id;
float marks;
};
int main() {
struct Student s1 = {101, 85.5};
printf("ID = %d, Marks = %.2f", [Link], [Link]);
return 0;
}

Example 2: Structure WITH typedef

#include <stdio.h>

typedef struct {
int id;
float marks;
} Student;
int main() {
Student s1 = {101, 85.5};
printf("ID = %d, Marks = %.2f", [Link], [Link]);
return 0;
}

No need to write struct Student again!

Example 3: typedef with Structure Name

#include <stdio.h>
typedef struct Student {
int id;
float marks;
} Student;
int main() {
Student s1 = {102, 90.0};
printf("ID = %d, Marks = %.2f", [Link], [Link]);
return 0;
}

Example 4: Multiple Variables

#include <stdio.h>
typedef struct {
int id;
char name[20];
} Employee;
int main() {
Employee e1 = {1, "Vidhya"};
Employee e2 = {2, "Rahul"};
printf("%d %s\n", [Link], [Link]);
printf("%d %s\n", [Link], [Link]);
return 0;
}

Key Advantages

 ✔ Makes code shorter and cleaner

 ✔ Improves readability

 ✔ Avoids repeated use of struct

 ✔ Useful in large programs and projects

What are Nested Structures?

A structure inside another structure is called a nested structure.

This is useful when one structure is logically part of another


(e.g., Address inside Student, Date inside Employee, etc.)
🔹 Two Ways to Create Nested Structures

WAY 1: Separate Structures (Dependent Structure)

Define structures separately, then include one inside another.

Example 1: Student with Address

#include <stdio.h>
// Inner structure
struct Address {
char city[20];
int pincode;
};
// Outer structure
struct Student {
int id;
char name[20];
struct Address addr; // nested structure
};

int main() {
struct Student s1 = {101, "Vidhya", {"Bangalore", 560067}};
printf("ID: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("City: %s\n", [Link]);
printf("Pincode: %d\n", [Link]);
return 0;
}

Output

ID: 101
Name: Vidhya
City: Bangalore
Pincode: 560067

Accessing Nested Members

Syntax:

outer_variable.inner_variable.member

Example:

[Link]
WAY 2: Embedded Structure (Direct Declaration)

Define structure inside another structure directly

Example 2: Employee with Date of Joining

#include <stdio.h>
struct Employee {
int id;
char name[20];
// Embedded structure
struct Date {
int day;
int month;
int year;
} doj; // variable of embedded structure
};
int main() {
struct Employee e1 = {1, "Rahul", {15, 3, 2026}};
printf("ID: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("DOJ: %d-%d-%d\n", [Link], [Link], [Link]);
return 0;
}

Output

ID: 1
Name: Rahul
DOJ: 15-3-2026

Mini Example

struct Date {
int d, m, y;
};

struct Student {
int id;
struct Date dob;
};
Passing Structure to a Function

Key Points

 ✔ In C, parameter passing is by value

 ✔ A copy of the structure is passed to the function

 ✔ Changes inside the function do NOT affect original data

 ✔ To modify original data → use pointer to structure

1. Passing Structure (By Value)

Structure is copied → original remains unchanged

Example: Read & Display Structure

#include <stdio.h>
// Structure declaration
struct Student {
int id;
float marks;
};
// Function to display structure
void display(struct Student s) {
printf("Inside function:\n");
printf("ID = %d, Marks = %.2f\n", [Link], [Link]);
// Modify copy
[Link] = 999;
}
int main() {
struct Student s1;
// Read values
printf("Enter ID and Marks: ");
scanf("%d %f", &[Link], &[Link]);
// Pass by value
display(s1);
printf("After function call:\n");
printf("ID = %d, Marks = %.2f\n", [Link], [Link]);
return 0;
}

Output (Sample)

Enter ID and Marks: 101 85.5


Inside function:
ID = 101, Marks = 85.50
After function call:
ID = 101, Marks = 85.50

Even though we changed [Link] = 999 inside function,


Original s1 is unchanged

2. Passing Structure Using Pointer (By Reference)

Pass address → original data can be modified

Example: Modify Structure Using Pointer

#include <stdio.h>
struct Student {
int id;
float marks;
};
// Function using pointer
void modify(struct Student *s) {
printf("Inside function before change:\n");
printf("ID = %d, Marks = %.2f\n", s->id, s->marks);
// Modify original data
s->id = 999;
s->marks = 100.0;
}
int main() {
struct Student s1 = {101, 85.5};
// Pass address
modify(&s1);
printf("After function call:\n");
printf("ID = %d, Marks = %.2f\n", [Link], [Link]);
return 0;
}

Output

Inside function before change:


ID = 101, Marks = 85.50
After function call:
ID = 999 Marks = 100.00Now changes are reflected because we passed
pointer

Important Syntax

✔ By Value

void func(struct Student s);


func(s1);

✔ By Pointer

void func(struct Student *s);


func(&s1);

Accessing Members

Metho Synta
d x

Normal [Link]

Pointer s->id
Array of Structures as a table or a database.

In C, a struct (structure) allows you to group different types of data (like a


name, an age, and a grade) into one "package." An array allows you to
store multiple packages of that same type.

The Real-World Analogy: An Office Cabinet

Imagine you have a filing cabinet for employees:

 The Structure: Is a single "Employee Folder" containing a Name,


ID, and Salary.

 The Array: Is the "Cabinet" that holds 50 of those folders in a


specific order (Folder 0, Folder 1, Folder 2...).

1. Define the "Package" (Structure)

First, you tell C what should be inside one single folder.

struct Student {
char name[50];
int roll_no;
float marks;
};

2. Create the "Cabinet" (Array of Structures)


Instead of creating student1, student2, and student3 individually, you create an array
that holds them all.

// This creates a "cabinet" that can hold 3 Student structures


struct Student class_list[3];

3. How to Access the Data

To get to a specific piece of information, you use the index (the folder
number) and the dot operator (the specific page in the folder).

 class_list[0] = The first student folder.

 class_list[0].marks = The marks of the first student.

Example Program

#include <stdio.h>

struct Student {

char name[20];

float marks;

};

int main() {

struct Student class[2]; // Array of 2 students

for(int i = 0; i < 2; i++) {

printf("Enter name for student %d: ", i + 1);

scanf("%s", class[i].name);

printf("Enter marks: ");

scanf("%f", &class[i].marks);

printf("\n--- Student Records ---\n");

for(int i = 0; i < 2; i++) {


printf("Student: %s | Marks: %.2f\n", class[i].name, class[i].marks);

return 0;

Why use this?

If you didn't use an array of structures, and you had 100 students, you
would have to create 100 different arrays for names and 100 different
arrays for marks. With this, everything stays grouped together logically

1. Store and Display Student Details

#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
struct Student s[2];
for(int i = 0; i < 2; i++) {
printf("Enter ID, Name, Marks:\n");
scanf("%d %s %f", &s[i].id, s[i].name, &s[i].marks);
}
printf("\nStudent Details:\n");
for(int i = 0; i < 2; i++) {
printf("ID: %d, Name: %s, Marks: %.2f\n",
s[i].id, s[i].name, s[i].marks);
}
return 0;
}
Output:

Enter ID, Name, Marks:


1 Ram 85.5
Enter ID, Name, Marks:
2 Sita 90.0

Student Details:
ID: 1, Name: Ram, Marks: 85.50
ID: 2, Name: Sita, Marks: 90.00

2. Find Highest Marks

#include <stdio.h>
struct Student {
char name[50];
float marks;
};
int main() {
struct Student s[3];
int maxIndex = 0;
for(int i = 0; i < 3; i++) {
printf("Enter Name and Marks:\n");
scanf("%s %f", &s[i].name, &s[i].marks);
}
for(int i = 1; i < 3; i++) {
if(s[i].marks > s[maxIndex].marks) {
maxIndex = i;
}
}

printf("\nTopper: %s with %.2f marks\n",


s[maxIndex].name, s[maxIndex].marks);

return 0;
}

Output:

Enter Name and Marks:


Ram 78
Sita 92
John 85

Topper: Sita with 92.00 marks

3. Employee Salary Calculation


#include <stdio.h>
struct Employee {
int id;
float basic, hra, da, gross;
};
int main() {
struct Employee e[2];
for(int i = 0; i < 2; i++) {
printf("Enter ID and Basic Salary:\n");
scanf("%d %f", &e[i].id, &e[i].basic);
e[i].hra = 0.2 * e[i].basic;
e[i].da = 0.1 * e[i].basic;
e[i].gross = e[i].basic + e[i].hra + e[i].da;
}
printf("\nEmployee Salary Details:\n");
for(int i = 0; i < 2; i++) {
printf("ID: %d, Gross Salary: %.2f\n",
e[i].id, e[i].gross);
}
return 0;
}

Output:

Enter ID and Basic Salary:


101 20000
Enter ID and Basic Salary:
102 30000

Employee Salary Details:


ID: 101, Gross Salary: 26000.00
ID: 102, Gross Salary: 39000.00

4. Sort Students by Marks

#include <stdio.h>
struct Student {
char name[50];
float marks;
};
int main() {
struct Student s[3], temp;
for(int i = 0; i < 3; i++) {
printf("Enter Name and Marks:\n");
scanf("%s %f", s[i].name, &s[i].marks);
}
for(int i = 0; i < 3; i++) {
for(int j = i + 1; j < 3; j++) {
if(s[i].marks > s[j].marks) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
printf("\nSorted List (Ascending):\n");
for(int i = 0; i < 3; i++) {
printf("%s - %.2f\n", s[i].name, s[i].marks);
}
return 0;
}

Output:

Enter Name and Marks:


Ram 78
Sita 92
John 85

Sorted List (Ascending):


Ram - 78.00
John - 85.00
Sita - 92.00

5. Search Student by ID

#include <stdio.h>
struct Student {
int id;
char name[50];
};
int main() {
struct Student s[3];
int searchId, found = 0;
for(int i = 0; i < 3; i++) {
printf("Enter ID and Name:\n");
scanf("%d %s", &s[i].id, s[i].name);
}
printf("Enter ID to search: ");
scanf("%d", &searchId);
for(int i = 0; i < 3; i++) {
if(s[i].id == searchId) {
printf("Found: %s\n", s[i].name);
found = 1;
break;
}
}
if(!found) {
printf("Student not found\n");
}
return 0;
}

Output:

Enter ID and Name:


1 Ram
2 Sita
3 John
Enter ID to search: 2

Found: Sita

You might also like