To understand and demonstrate the use of pointers to structures in C programming by constructing and executing a simple C program. This lab will help students grasp how pointers can be used to reference and manipulate data within structures.
Table of contents
Open Table of contents
- Instructions
- 1. Create a Structure
- 2. Declare a Pointer to the Structure
- 3. Assign Pointer to Structure Variable
- 4. Access Structure Members Using the Pointer
- 5. Display the Structure Data via Pointer
- 6. Compile and Execute
- Expected Output
- Explanation
- Spoiler
- Conclusion
- Key Takeaways
- Exercises
- References
Instructions
- Go to s-m-quadri/
geca-labs
repository. - Switch branch to
CSPC2005/01
corresponding to current lab. - Click on
CODE
, thenCodespaces
, thenCreate
, to setup in-browser IDE. - Wait until VS Code gets open.
- Write program from scratch, compile, execute. Then only tally with already available solution.
IMP: Make sure to delete after completion of lab. Otherwise your monthly free-hours limit can exhaust. It can be achieved by:
- Closing in-browser
VS Code
tab. - Click on
CODE
, thenCodespaces
, thenDelete
, to delete virtual space.
- Closing in-browser
1. Create a Structure
- Define a structure to represent a
Student
with fields such asname
,age
, andgrade
.
#include <stdio.h>
// Define structure
struct Student {
char name[50];
int age;
float grade;
};
2. Declare a Pointer to the Structure
- Declare a structure variable and a pointer to that structure.
int main() {
// Declare structure variable
struct Student student1;
// Declare a pointer to structure
struct Student *ptr;
}
3. Assign Pointer to Structure Variable
- Initialize the pointer by assigning it the address of the structure variable
student1
.
// Assign the address of student1 to the pointer
ptr = &student1;
4. Access Structure Members Using the Pointer
- Use the pointer to access and modify structure members.
// Accessing members using the pointer
printf("Enter student's name: ");
scanf("%s", ptr->name);
printf("Enter student's age: ");
scanf("%d", &ptr->age);
printf("Enter student's grade: ");
scanf("%f", &ptr->grade);
5. Display the Structure Data via Pointer
- Use the pointer to display the contents of the structure.
// Display data using pointer
printf("\nStudent Information:\n");
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Grade: %.2f\n", ptr->grade);
return 0;
}
6. Compile and Execute
- Compile the program using a C compiler. If you are using GCC, the command will be:
gcc pointer_to_structure.c
- Run the executable:
./a.out
Expected Output
Enter student's name: John
Enter student's age: 21
Enter student's grade: 85.5
Student Information:
Name: John
Age: 21
Grade: 85.50
Explanation
struct Student
defines the structure with members:name
,age
, andgrade
.- Pointer
ptr
is used to store the address of thestudent1
structure. - The
->
operator is used to access structure members via a pointer. - Data is input using
scanf()
and accessed or printed using the pointer.
Spoiler
(Full source code) Do not copy-paste
#include <stdio.h>
// Define structure
struct Student {
char name[50];
int age;
float grade;
};
int main() {
// Declare structure variable
struct Student student1;
// Declare a pointer to structure
struct Student *ptr;
// Assign the address of student1 to the pointer
ptr = &student1;
// Accessing members using the pointer
printf("Enter student's name: ");
scanf("%s", ptr->name);
printf("Enter student's age: ");
scanf("%d", &ptr->age);
printf("Enter student's grade: ");
scanf("%f", &ptr->grade);
// Display data using pointer
printf("\nStudent Information:\n");
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Grade: %.2f\n", ptr->grade);
return 0;
}
Conclusion
In this lab, we successfully implemented a C program to demonstrate the use of pointers with structures. You learned how to declare a pointer to a structure, access structure members using the pointer, and manipulate structure data dynamically.
Key Takeaways
- Pointers provide efficient ways to access and manipulate data within structures.
- The
->
operator is essential for accessing structure members via pointers. - Pointers to structures are commonly used in dynamic memory management and complex data handling tasks.
Exercises
- Modify the program to store details for multiple students using an array of structures.
- Implement a function that takes a pointer to a structure as an argument and modifies the structure’s values.
This concludes the lab on using pointers to structures in C.