CSE 5 Lab Report
CSE 5 Lab Report
: 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;
}
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("\n");
}
printf("\nInformation of Students:\n");
for(int i = 0; i < 5; i++) {
printf("Student %d:\n", i + 1);
printf("\n");
}
return 0;
}
2|Page
Output
Problem 1:
Problem 2:
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