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

CSE 5 Lab Report

The document outlines an experiment focused on understanding pointers and structures in C programming. It includes objectives such as file handling and managing student records using structures, along with example code for writing to a file and displaying student information. The conclusion emphasizes the importance of mastering these concepts for efficient data management and program optimization.
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 views

CSE 5 Lab Report

The document outlines an experiment focused on understanding pointers and structures in C programming. It includes objectives such as file handling and managing student records using structures, along with example code for writing to a file and displaying student information. The conclusion emphasizes the importance of mastering these concepts for efficient data management and program optimization.
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
You are on page 1/ 4

Experiment No.

: 05
Name of the Experiment
Introduction to Pointers and Struct Data Types in C Programming.
Objectives
Objectives for this lab are:
 Understanding the application of pointers.
 Learn how to open, write to, and close files in C.
 Practice storing multiple related pieces of information (name, roll number, CGPA) of
different data types in a single structure.
Introduction
This lab focuses on two fundamental concepts in C programming: “Pointers” and
“Structures”. Pointers, a core feature of C, allow for direct memory access and manipulation,
making them essential for efficient memory management and dynamic data structures. The
first experiment explores the use of pointers to interact with memory addresses, enabling
efficient data storage, retrieval, and manipulation.
Structures, on the other hand, provide a way to group related variables of different data types
under a single entity. The second experiment demonstrates how to use structures to manage
complex data, such as student records, and highlights the role of pointers in accessing and
modifying structure members. Understanding the relationship between pointers and structures
is crucial for building optimized programs that handle complex data efficiently.
Code Section
Problem 1: Write a c program to write a single line to a text file.
Solution:
# include<stdio.h>

int main(){
FILE *myfile = fopen("nfs_most_wanted.txt", "w");

if (myfile == NULL){
printf("Error loading file!\n");
return 1;
}

fprintf(myfile,"I need a Posrche 911.\n");


fclose(myfile);

return 0;
}

1|Page
Problem 2: Write a c program that will display the information (name, roll, CGPA) of 5
students using a structure. All the value will be given by the user.
Solution:
#include <stdio.h>

struct students{
char name[50];
int roll;
float cgpa;
};
int main() {
struct students eachStudent[5];
for (int i = 0; i < 5; i++){

printf("Enter information for student %d:\n", i+1);

printf("Enter name: ");


scanf(" %[^\n]s", eachStudent[i].name);

printf("Enter roll: ");


scanf("%d", &eachStudent[i].roll);

printf("Enter CGPA: ");


scanf("%f", &eachStudent[i].cgpa);

printf("\n");
}
printf("\nInformation of Students:\n");
for(int i = 0; i < 5; i++) {
printf("Student %d:\n", i + 1);

printf("Name: %s\n", eachStudent[i].name);

printf("Roll Number: %d\n", eachStudent[i].roll);

printf("CGPA: %.2f\n", eachStudent[i].cgpa);

printf("\n");
}

return 0;
}

2|Page
Output
Problem 1:

Figure 1: Problem 1 Terminal Output

Problem 2:

Figure 2: Problem 2 Terminal Output

Result
The codes ran successfully and the output is shown above.

3|Page
Discussion
There were some errors for the second code. “Enter Roll and “Enter CGPA were printed on
the same row. It was solved by putting a space in front of “%[^\n]s”. For the first problem,
the output doesn’t show anything as there is no text file with the given name.
Conclusion
This lab provided an in-depth understanding of pointers and structures in C programming.
The experiments demonstrated how pointers are crucial for efficient memory management,
allowing direct manipulation of memory addresses and enabling dynamic data handling. The
use of pointers in conjunction with structures showcased the importance of these concepts in
organizing and accessing complex data efficiently.
By applying pointers to structure members, the lab highlighted their role in enhancing data
access and manipulation capabilities in programs. Mastery of pointers and structures is
essential for writing optimized, modular, and efficient C programs, particularly when
managing large sets of data or building dynamic applications.

4|Page

You might also like