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

C Programming Lab Report - 3

This lab report details various experiments conducted in C programming, focusing on pointers, structures, and dynamic memory allocation. Key experiments include declaring and initializing pointers, demonstrating different pointer types, passing pointers to functions, and understanding the relationship between pointers and arrays. The report also includes lab assignments on swapping values using pointers, finding the largest integer using pointers, and adding two matrices with pointer arithmetic.

Uploaded by

samirchhetri7799
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 views40 pages

C Programming Lab Report - 3

This lab report details various experiments conducted in C programming, focusing on pointers, structures, and dynamic memory allocation. Key experiments include declaring and initializing pointers, demonstrating different pointer types, passing pointers to functions, and understanding the relationship between pointers and arrays. The report also includes lab assignments on swapping values using pointers, finding the largest integer using pointers, and adding two matrices with pointer arithmetic.

Uploaded by

samirchhetri7799
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

Lab Report

Of C-Programming
Subject Code: CSC-115

Submitted To
Prithvi Raj Paneru
Birendra Multiple Campus
(Affiliated to Tribhuvan University)
Bharatpur, Chitwan

Submitted By:
Aaditya Timalsina
Section: A
Program: [Link]. CSIT
Semester: First (1st)
Index
Date of Date of
Title Signature
Experiment Submission
Lab 5: Pointers
Exp 1: To declare and initialize pointers
Exp 2: To demonstrate various types of
pointers

Exp 3: To pass and return pointer in a


function

Exp 4: To demonstrate relationship between


pointer and function

Exp 5: To demonstrate dynamic memory


allocation

Lab Assignment
Lab 6: Structure
Exp 1: To define, declare, initialize, and
access structure variables

Exp 2: To pass structure to a function


Exp 3: To pass structure pointer to a
function
Exp 4: To pass structure array to a function
Lab Assignment

Lab 5: Pointers
Experiment 1: To declare and initialize pointers

1. Objectives
• To understand the concept of pointers in C programming.
• To learn how to initialize and declare pointers.
• To access and display values using pointers.

2. Theory
A pointer is a variable that stores the memory address of another variable. Instead of holding
a direct value, it holds the location where the value is stored.

Syntax
data_type *pointer_name;

Example
int *ptr;

Initialization of Pointer
A pointer is initialized by assigning it the address of a variable using the address-of (&)
operator.

int a = 10; int *ptr = &a;

Accessing Value Using Pointer


The value stored at the address can be accused using the dereference (*) operator.

*ptr

3. Demonstration
To understand one-dimensional arrays, I wrote a program to declare, initialize, and access
elements of an array.

Example Code
l5e1.c
#include <stdio.h>
int main()
{
int a = 10;
int *ptr = &a;
printf("Value: %d\nAddress of a: %p\n", a, &a);
printf("\nValue of pointer: %p\nValue at address: %d\nAddress of ptr: %p\n", ptr, *ptr,
&ptr);
a = 20;
printf("\nAfter Changing value of a:\n");
printf("Value: %d\nAddress of a: %p\n", a, &a);
printf("\nValue of pointer: %p\nValue at address: %d\nAddress of ptr: %p\n", ptr, *ptr,
&ptr);
return 0;
}

4. Output and Discussion

Here, we can see that the value at the address pointed towards by the pointer (given by *ptr)
also changes its value when value of variable a is changed.

5. Conclusion
In this experiment, I learned about pointers in C and how they store memory addresses of
variables. I learned how to declare and initialize pointers using the address-of (&) operator,
and how to access values using the dereference (*) operator.

Experiment 2: To Demonstrate Various Types of Pointers


1. Objectives
• To understand different types of pointers in C.
• To learn how to declare and use various types of pointers.
• To demonstrate their behavior through examples.

2. Theory
Pointers in C can be classified into different types based on their usage and behavior.

Types of Pointers:

1. Null Pointer
A pointer that does not point to any valid memory location. It is assigned
NULL.
int *ptr = NULL;
2. Void Pointer
A generic pointer that can store the address of any data type. It must be
typecast before dereferencing.
void *ptr;
3. Wild/Bad Pointer
A pointer that is declared but not initialized. It may point to any random
memory location.
int *ptr;
4. Dangling Pointer
A pointer that points to memory that has been freed or deleted.
5. Pointer to Pointer
A pointer that stores the address of another pointer.
Int **ptr;

3. Demonstration
l5e2.c
#include <stdio.h>
int main()
{
//Use of Null Pointer is shown here
int *p1 = NULL;
printf("Null Pointer p1: %p\n", p1);

//Use of Void Pointer is shown here


int a = 10;
void *p2;
p2 = &a;
printf("Void Pointer: %d\n", *(int *)p2);

//Use of Bad Pointer is shown here


int *p3;
printf("Bad Pointer: %p\n", p3);

//Use of Pointer to Pointer is shown here


int b = 20;
int *p5 = &b;
int **p6 = &p5;
printf("Value using pointer to pointer: %d\n", **p6);
return 0;
}

4. Output and Discussion

This demonstrated how a NULL Pointer printed empty value, Void Pointer worked after
typecasting, Bad/Wild Pointer gave garbage value, and Pointer to Pointer stored values used
double dereference (**) operators.

5. Conclusion
In this experiment, I learned about different types of pointers in C and their behavior. I
understood how null, void, bad, dangling, and pointer to pointer, etc. are used.

Experiment 3: To Pass and Return Pointer in a Function


1. Objectives
• To understand how pointers are passed to functions.
• To demonstrate modification of values using pointers.
• To learn how to return pointers from functions.

2. Theory
A pointer can be passed to functions to allow direct modification of variables. Instead of
passing values (copy), we pass addresses, which enables functions to work with original data.

Passing Pointer to Function


When a pointer is passed, the function receives the address of the variable and can modify its
value.
void func(int *i);

Returning Pointer from Function


A function can return a pointer.
 Do not return addresses of local variable as they are removed from memory after
function ends.
 Return pointer to static or dynamically allocated memory.

int* func();
3. Demonstration
l5e3.c
#include <stdio.h>
void square(int *p)
{
*p = *p * *p;
}
int *getPointer()
{
static int x = 50;
return &x;
}
int main()
{
int a = 20;
printf("Before update: %d\n", a);
square(&a);
printf("After update: %d\n", a);
int *ptr;
ptr = getPointer();
printf("Value returned from function: %d\n", *ptr);
return 0;
}

4. Output and Discussion

As we can see, passing pointers to function helped us to modify original value instead of
reassigning the variable.

5. Conclusion
In this experiment, I learned how to pass pointers to functions and return pointers from
functions in C. I understood that passing pointers allows modification of original variables,
and returning pointers requires careful handling to avoid invalid memory access.

Experiment 4: To Demonstrate Relationship Between Pointers


and Arrays
1. Objectives
• To understand the relation between pointers and arrays.
• To learn how arrays are accessed using pointers.
• To demonstrate pointer arithmetic with arrays.

2. Theory
In C, an array name itself acts as a pointer to the first element of the array.

For Example:
int arr[5];
arr represents the address of the first element: &arr[0]

Therefore:
arr == &arr[0]

Pointer and Array Relationship


• arr[i] is equivalent to *(arr + i)
• Pointer arithmetic allows traversal of array elements

For Example:
arr[2] == *(arr + 2)

Pointer Arithmetic
• arr + 1 moves to next element
• It increases by size of data type (e.g., 4 bytes for int)

3. Demonstration
l5e4.c
#include <stdio.h>
int main()
{
int numbers[5];
printf("Enter numbers:\n");
for (int i = 0; i < 5; i++)
{
scanf("%d", numbers + i); // equivalent to &numbers[i]
}
printf("\nPrinting numbers using pointer arithmetic:\n");
for (int i = 0; i < 5; i++)
{
printf("%d\t", *(numbers + i)); // equivalent to numbers[i]
}

printf("\n\nValue of array, address of first item of array: %p %p", numbers,


&numbers[0]);

return 0;
}

4. Output and Discussion

Value of array, and address of first item are same. We can also see that pointer arithmetic is
just behind the scenes of syntactical sugar that we usually use.

5. Conclusion
In this experiment, I learned the relationship between pointers and arrays in C. I understood
that the array name acts as a pointer to the first element, and elements can be accessed using
both indexing and pointer arithmetic.

Experiment 5: To Demonstrate Dynamic Memory Allocation


1. Objectives
• To understand dynamic memory allocation in C
• To learn the use of malloc(), calloc(), realloc() and free()

2. Theory
Dynamic Memory Allocation (DMA) allows memory to be allocated at runtime instead of
compile time. It is useful when the size of data is not known beforehand. DMA functions are
defined in the header file:

#include <stdlib.h>

Note: Some old codebases often use <malloc.h> instead of <stdlib.h>. <malloc.h> is
outdated, nonstandard and deprecated. <stdlib.h> is part of C standard and recommended to
use. Both work similarly.

Important Functions
1. malloc()
a. Allocates memory of given size
b. Returns pointer to allocated memory
c. Memory is uninitialized (garbage values)
ptr = (type*) malloc(size);

2. calloc()
a. Allocates memory for multiple elements
b. Initializes memory to zero

ptr = (type*) calloc(n, size);

3. realloc()
a. Resizes previously allocated memory

ptr = realloc(ptr, new_size);

4. free()
a. Deallocates memory to prevent memory leaks

free(ptr);
3. Demonstration
l5e5.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr, n, i;

printf("Enter number of elements : ");


scanf("%d", &n);

// malloc
arr = (int *)malloc(n * sizeof(int));

printf("Enter %d elements :\n", n);


for (i = 0; i < n; i++)
scanf("%d", &arr[i]); // can also use pointer arithmetic

printf("Elements using malloc :\n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

// realloc
printf("\nEnter new size : ");
scanf("%d", &n);

arr = (int *)realloc(arr, n * sizeof(int));

printf("Enter new elements :\n");


for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Elements after realloc :\n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

// calloc
arr = (int *)calloc(n, sizeof(int));
printf("\nInitialized values of calloc:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);

// free memory
free(arr);
return 0;
}

4. Output and Discussion

5. Conclusion
In this experiment, I learned how to allocate, reallocate, and deallocate memory dynamically
using malloc(), realloc(), and free().
Lab Assignments 5
A) To Swap Value of Two Variables Using Pointers

1. Objectives
• To understand swapping using pointers
• To learn pass by reference in C

2. Theory
Swapping means exchanging the values of two variables. In C, swapping can be done in two
ways:

• Pass by Value: does not change original values


• Pass by Reference (using pointers): changes original values

When we use pointers, we pass the addresses of variables, allowing direct modification of
original data.

Logic
• Store value of first variable in a temporary variable
• Assign second variable to first
• Assign temporary value to second
Syntax

void swap(int *a, int

*b);

3. Demonstration
l5a1.c
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Before swap: x: %d, y: %d\n", x, y);
swap(&x, &y);
printf("After swap: x: %d, y: %d\n", x, y);
return 0;
}

4. Output and Discussion

5. Conclusion
In this experiment, I learned how to swap two variables using pointers in C. I understood that
passing addresses to a function allows direct modification of original variables.

B) To Find Largest Among Given n Integers Using Pointer

1. Objectives
• To find the largest element using pointer arithmetic
• To understand that array name acts as a pointer

2. Theory
In C, the array name itself behaves like a pointer to the first element.

arr is equivalent to &arr[0]

Elements can be accessed using pointer arithmetic:

*(arr + i) is same as arr[i]

3. Demonstration
l5a2.c
#include <stdio.h>
int main()
{
int arr[100], n, i, max;
printf("Enter number of elements : ");
scanf("%d", &n);
printf("Enter %d elements :\n", n);
for (i = 0; i < n; i++)
scanf("%d", arr + i);
max = *arr;
for (i = 1; i < n; i++)
if (*(arr + i) > max)
max = *(arr + i);
printf("Largest element : %d\n", max);
return 0;
}

4. Output and Discussion

Here, I used pointer arithmetic to find maximum number of n numbers in array.

5. Conclusion
In this experiment, I learned how to find the largest element using pointer arithmetic.

C) To Add Two Matrices Using Pointer and Function

1. Objectives
• To perform matrix addition using pointers
• To use functions with pointers
• To apply pointer arithmetic in 2D arrays
2. Theory
In C, a 2D array is stored in row-major order, meaning elements are stored row by row in
contiguous memory. The array name represents the base address, so pointer arithmetic can be
used to access elements.

For a matrix:
*(*(a + i) + j) is equivalent to a[i][j]

3. Demonstration

l5a3.c
#include <stdio.h>
#define max 10

void read(int a[][max], int r, int c)


{
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", *(a + i) + j);
}
}

void add(int a[][max], int b[][max], int sum[][max], int r, int c)


{
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
*(*(sum + i) + j) = *(*(a + i) + j) + *(*(b + i) + j);
}

void display(int a[][max], int r, int c)


{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
printf("%d\t", *(*(a + i) + j));
printf("\n");
}
}
int main()
{
int a[max][max], b[max][max], sum[max][max];
int r, c;

printf("Enter rows and columns:");


scanf("%d %d", &r, &c);

printf("Enter first matrix:\n");


read(a, r, c);

printf("Enter second matrix:\n");


read(b, r, c);

add(a, b, sum, r, c);

printf("Sum of matrices:\n");
display(sum, r, c);

return 0;
}

4. Output and Discussion

5. Conclusion
In this experiment, I learned how to add two matrices using pointers and functions. I
understood how pointer arithmetic can be applied to two-dimensional arrays.
D) To Read n Integers Dynamically and Find Their Sum

1. Objectives
• To allocate memory dynamically using functions
• To read array elements using pointers
• To compute sum using pointer arithmetic

2. Theory
Dynamic Memory Allocation allows memory to be allocated at runtime using malloc().
When an array is passed to a function:

• Its base address is passed


• The function works on the original memory

In terms of arrays and pointers:

*(ptr+i) is equivalent to arr[i]

Logic
• Allocate memory using malloc()
• Pass pointer to functions
• Read elements using pointer arithmetic
• Compute sum in a separate function
• Free memory after use

3. Demonstration

l5a4.c
#include <stdio.h>
#include <stdlib.h>

void read(int numbers[], int n)


{
for (int i = 0; i < n; i++)
scanf("%d", (numbers + i));
}

int sum(int numbers[], int n)


{
int s = 0;
for (int i = 0; i < n; i++)
s += *(numbers + i);
return s;
}
int main()
{
int n, result;

printf("Enter number of elements:");


scanf("%d", &n);

int *numbers = (int *)malloc(n * sizeof(int));


printf("Enter %d elements:\n", n);
read(numbers, n);

result = sum(numbers, n);


printf("\nSum: %d\n", result);

free(numbers);

return 0;
}

4. Output and Discussion

5. Conclusion
In this experiment, I learned how to use dynamic memory allocation along with functions to
read n integers and calculate their sum.
Lab 6: Structure
Experiment 1: To Define, Declare, Initialize and Access Structure
Variable

1. Objectives
• To understand structures in C
• To define and declare structure variables
• To initialize and access structure members

2. Theory
A structure in C is a user-defined data type that allows grouping variables of different data
types under a single name. It is useful for representing a record.

Syntax
struct structure_name {

data_type member1;

data_type member2;

};

Declaration of Structure Variable

struct structure_name var;

Initialization

struct student stu = {1,"Aaditya",99};

Accessing Members
Members are accessed using the dot operator (.)

[Link]
[Link]
[Link]
3. Demonstration
l6e1.c
#include <stdio.h>

struct student
{
int id;
char name[20];
float marks;
};
int main()
{
struct student stu;

printf("Student id: ");


scanf("%d", &[Link]);
printf("Name: ");
scanf("%s", &[Link]);
printf("Marks: ");
scanf("%f", &[Link]);

printf("\nStudent details:\n");
printf("Id: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("Marks: %f\n", [Link]);

return 0;
}

4. Output and Discussion

Structure is a collection of different data types.


5. Conclusion
In this experiment, I learned how to define, declare, initialize, and access structure variables
in C. I understood how structures help in organizing related data efficiently and make
programs more structured.

Experiment 2: To Pass Structure to a Function

1. Objectives
• To understand passing structure to a function
• To learn how structure data is copied during function call
• To display structure values using a function

2. Theory
A structure can be passed to a function in two ways:

1. Passing Entire Structure


a. Structure is passed as a whole
b. A copy of structure is created
c. Changes inside function do not affect original data
2. Passing Members Separately
a. Individual members are passed as arguments
b. Works like normal variable passing
c. Also follows pass by value (copy)
• Both methods use pass by value
• Entire structure passing copies all members at once
• Member-wise passing copies values individually
• Passing only members is useful in cases where structure is huge
3. Demonstration
l6e2.c
#include <stdio.h>
struct student
{
int id;
char name[20];
float marks;
};

void displayStruct(struct student stu)


{
printf("\nUsing structure:\n");
printf("Id: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("Marks: %f\n", [Link]);
}

void displayMembers(int id, char name[], float marks)


{
printf("\nUsing members:\n");
printf("ID: %d\n", id);
printf("Name: %s\n", name);
printf("Marks: %f\n", marks);
}
int main()
{
struct student stu;

printf("Enter Id: ");


scanf("%d", &[Link]);
printf("Enter Name: ");
scanf("%s", &[Link]);
printf("Enter Marks: ");
scanf("%f", &[Link]);

displayStruct(stu);
displayMembers([Link], [Link], [Link]);

return 0;
}
4. Output and Discussion

5. Conclusion
In this experiment, I learned how to pass a structure to a function in C. I understood that
when a structure is passed, a copy of its data is created, and any modifications inside the
function do not affect the original structure.

Experiment 3: To Pass Structure Pointer to a Function

1. Objectives
• To understand passing structure using pointers
• To read structure values using a function
• To display structure values using pointer

2. Theory
A structure can be passed to a function using a pointer. Instead of passing a copy, we pass the
address of structure. This is called pass by reference. Changes inside the function affect
original data
Accessing Members Using Pointer
When using structure pointer, members are accessed using:

ptr->member

This is equivalent to:

(*ptr).member

3. Demonstration
l6e3.c
#include <stdio.h>

struct student
{
int id;
char name[20];
float marks;
};

void read(struct student *stu)


{
printf("Enter Id: ");
scanf("%d", &stu->id);
printf("Enter Name: ");
scanf("%s", &stu->name);
printf("Enter Marks: ");
scanf("%f", &stu->marks);
}

void display(struct student *stu)


{
printf("\nStudent details:\n");
printf("Id: %d\n", stu->id);
printf("Name: %s\n", stu->name);
printf("Marks: %f\n", stu->marks);
}
int main()
{
struct student stu;
read(&stu);
display(&stu);
return 0;
}

4. Output and Discussion

5. Conclusion
In this experiment, I learned how to pass a structure using pointers in C. I understood that
passing a pointer avoids copying of data and allows direct access and modification of the
original structure.

Experiment 4: To Pass Structure Array to a Function

1. Objectives
• To understand array of structures
• To pass structure array to functions
• To read and display n structure records

2. Theory
An array of structures allows storing multiple records of the same type. When a structure
array is passed to a function:

• The base address of the array is passed


• It behaves like passing a pointer
• No complete copy of array is made
Syntax

struct student s[10];

Passing Structure Array

void func(struct student s[], int n);

3. Demonstration
l6e4.c
#include <stdio.h>
#include <stdlib.h>

struct Student
{
int id;
char name[20];
float marks;
};

void read(struct Student stu[], int n)


{
for (int i = 0; i < n; i++)
{
printf("\nEnter details of student
%d:\n", i + 1);
printf("Id: ");
scanf("%d", &stu[i].id);
printf("Name: ");
scanf("%s", &stu[i].name);
printf("Marks: ");
scanf("%f", &stu[i].marks);
}
}

void display(struct Student stu[], int n)


{
printf("\nStudent details:\n");
for (int i = 0; i < n; i++)
{
printf("\nStudent %d:\n", i + 1);
printf("Id: %d\n", stu[i].id);
printf("Name: %s\n", stu[i].name);
printf("Marks: %f\n", stu[i].marks);
}
}
int main()
{
int n;
printf("Enter number of students: ");
scanf("%d", &n);

struct Student *students = (struct


Student *)malloc(n * sizeof(struct
Student));
read(students, n);
display(students, n);

return 0;
}

4. Output and Discussion


5. Conclusion
In this experiment, I learned how to use an array of structures and pass it to functions. I
understood that only the base address is passed, allowing efficient handling of multiple
records without copying the entire array.

Lab Assignments 6
A) Create a Structure Named Student Having Members id, name,
contact and marks. WAP to Read Records of n Students, Find the
Average Marks and Display the Records Entered

1. Objectives
• To create a structure for student records
• To allocate memory dynamically using malloc()
• To read and display n student records using functions
• To calculate average marks

2. Theory
A structure can be used to store multiple attributes of a student such as id, name, contact, and
marks.

3. Demonstration
l6a1.c
#include <stdio.h>
#include <stdlib.h>

struct Student
{
int id;
char name[20];
char contact[15];
float marks;
};

void read(struct Student students[], int n)


{
for (int i = 0; i < n; i++)
{
printf("\nEnter details of student %d:\n", i + 1);
printf("Id: ");
scanf("%d", &students[i].id);
printf("Name: ");
scanf("%s", students[i].name);
printf("Contact: ");
scanf("%s", students[i].contact);
printf("Marks: ");
scanf("%f", &students[i].marks);
}
}

float average(struct Student students[], int n)


{
float sum = 0;
for (int i = 0; i < n; i++)
sum += students[i].marks;
return sum / n;
}

void display(struct Student students[], int n)


{
printf("\nStudent Records:\n");
for (int i = 0; i < n; i++)
{
printf("\nStudent %d:\n", i + 1);
printf("Id: %d\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Contact: %s\n", students[i].contact);
printf("Marks: %f\n", students[i].marks);
}
}
int main()
{
struct Student *students;
int n;
float avg;

printf("Enter number of students: ");


scanf("%d", &n);

students = (struct Student *)malloc(n * sizeof(struct Student));


read(students, n);

avg = average(students, n);


display(students, n);
printf("\nAverage marks: %f\n", avg);

free(students);

return 0;
}

4. Output and Discussion


5. Conclusion
In this program, I learned how to use structures with dynamic memory allocation to store and
manage multiple student records. I also learned how to calculate average marks and display
the records efficiently using functions.

B) Create a Structure Named Book Having Members title,


author, pages and price. WAP to Read Records of n Books and
Display Records Using Function

1. Objectives
• To create a structure for storing book details
• To read records of n books using functions
• To display records using functions

2. Theory
A structure is used to group related data of different types.

3. Demonstration
l6a2.c
#include <stdio.h>
#include <stdlib.h>
struct Book
{
char title[50];
char author[20];
int pages;
float price;
};

void read(struct Book b[], int n)


{
for (int i = 0; i < n; i++)
{
printf("\nEnter details of book %d:\n", i + 1);
printf("Title: ");
scanf("%s", b[i].title);
printf("Author: ");
scanf("%s", b[i].author);
printf("Pages: ");
scanf("%d", &b[i].pages);
printf("Price: ");
scanf("%f", &b[i].price);
}
}

void display(struct Book b[], int n)


{
printf("\nBook Records:\n");
for (int i = 0; i < n; i++)
{
printf("\nBook %d:\n", i + 1);
printf("Title: %s\n", b[i].title);
printf("Author: %s\n", b[i].author);
printf("Pages: %d\n", b[i].pages);
printf("Price: %f\n", b[i].price);
}
}
int main()
{
int n;
printf("Enter number of books: ");
scanf("%d", &n);

struct Book *books = (struct Book *)malloc(n * sizeof(struct Book));


read(books, n);
display(books, n);

return 0;
}
4. Output and Discussion

5. Conclusion
In this program, I learned how to create a structure and store multiple records using an array
of structures. I also learned how to use functions to read and display records efficiently.
C) Create a Structure Named Employee Having Members id,
name and salary. Read Records of n Employee, Increment Salary
of Each Employee by 15% and Display Records Using Pointers
and Function

1. Objectives
• To create a structure for employee records
• To use pointers with structure arrays
• To modify data using functions (pass by reference)

2. Theory
A structure is used to group employee details like id, name, and salary.

3. Demonstration
l6a3.c
#include <stdio.h>
#include <stdlib.h>

struct Employee
{
int id;
char name[20];
float salary;
};

void read(struct Employee e[], int n)


{
for (int i = 0; i < n; i++)
{
printf("\nEnter details of employee %d:\n", i + 1);
printf("Id: ");
scanf("%d", &(e + i)->id);
printf("Name: ");
scanf("%s", (e + i)->name);
printf("Salary: ");
scanf("%f", &(e + i)->salary);
}
}
void increment(struct Employee e[], int n)
{
for (int i = 0; i < n; i++)
e[i].salary = e[i].salary + 0.15 * e[i].salary;
}

void display(struct Employee e[], int n)


{
printf("\n\nEmployee Record after incrementing:\n");
for (int i = 0; i < n; i++)
{
printf("\nEmployee %d:\n", i + 1);
printf("ID: %d\n", (e + i)->id);
printf("Name: %s\n", (e + i)->name);
printf("Salary: %f\n", (e + i)->salary);
}
}
int main()
{
int n;
printf("Enter number of employees: ");
scanf("%d", &n);
struct Employee *e = (struct Employee *)malloc(n * sizeof(struct Employee));

read(e, n);
increment(e, n);
display(e, n);

free(e);

return 0;
}
4. Output and Discussion

As we can see, it increased the salary of each employees by 15%.

5. Conclusion
In this program, I learned how to use pointers with structure arrays and functions to modify
data.

D) To Demonstrate Use of Nested Structure

1. Objectives
• To understand nested structures in C
• To use functions to read and display data
• To access nested structure members

2. Theory
A nested structure is a structure that contains another structure as its member. It helps
organize complex data.
This is useful when data has sub-related fields, for example:

• A student has personal details and address


• Address itself contains multiple fields

Syntax

struct Outer {
struct Inner {
// members
} var;
};

Accessing Members
• Outer member: [Link]
• Nested member: [Link]

3. Demonstration
#include <stdio.h>

struct Address
{
char city[20];
char country[20];
};

struct Student
{
int id;
char name[20];
struct Address addr;
};

void read(struct Student *s)


{
printf("Enter Id: ");
scanf("%d", &s->id);
printf("Enter Name: ");
scanf("%s", &s->name);
printf("Enter City: ");
scanf("%s", &s->[Link]);
printf("Enter Country: ");
scanf("%s", &s->[Link]);
}

void display(struct Student *s)


{
printf("\nStudent Details:\n");
printf("Id: %d\n", s->id);
printf("Name: %s\n", s->name);
printf("City: %s\n", s->[Link]);
printf("Country: %s\n", s->[Link]);
}
int main()
{
struct Student s;
read(&s);
display(&s);

return 0;
}

4. Output and Discussion

5. Conclusion
In this program, I learned how to use nested structures along with functions in C. I
understood how to read and display data using pointers and how nested members can be
accessed efficiently.

You might also like