Pointer Additional Problems :
Problem 1: Basic pointer to an integer
Question:
Write a C program to:
Declare an integer variable and a pointer to integer.
Store the address of the integer in the pointer.
Print the value of the integer, its address, and the value accessed through the
pointer.
Why/What/How?
Why: To understand what a pointer really stores (an address).
What: A pointer to int that stores the address of an int.
How: Use & to get the address and * to get the value.
#include <stdio.h>
int main() {
int num = 10; // normal integer variable
int *p; // pointer to int
p = # // store address of num in p
printf("Value of num = %d\n", num);
printf("Address of num = %p\n", (void *)&num);
printf("Value of p (address)= %p\n", (void *)p);
printf("Value at *p = %d\n", *p);
return 0;
Explanation:
int *p; declares a pointer variable that can store the address of an int.
p = # stores the address of num into p.
&num means “address of num”.
*p means “value stored at the address inside p”.
We print addresses using %p and cast to (void *) (standard practice).
Problem 2: Change a variable using a pointer
Question:
Write a program that reads an integer from the user using scanf, and then uses a pointer
to increase its value by 5. Print the original and updated values.
Why/What/How?
Why: To see that changing *p changes the original variable.
What: Use pointer to modify the variable.
How: Let pointer point to the variable and then do *p = *p + 5.
#include <stdio.h>
int main() {
int x;
int *px;
printf("Enter an integer: ");
scanf("%d", &x);
printf("Original value of x = %d\n", x);
px = &x; // px points to x
*px = *px + 5; // increase value at address by 5
printf("Updated value of x = %d\n", x);
return 0;
Explanation:
scanf("%d", &x); needs the address of x, so we use &x.
px = &x; now px stores the address of x.
*px is another way of saying “x”.
When we do *px = *px + 5;, we are actually modifying x.
Problem 3: Swap two numbers using pointers and a function
Question:
Write a function swap(int *a, int *b) that swaps the values of two integers. In main, read
two numbers, call swap, and print the result.
Why/What/How?
Why: To understand how pointers allow functions to modify variables in main.
What: Function arguments are addresses of the variables.
How: Swap *a and *b using a temporary variable.
#include <stdio.h>
void swap(int *a, int *b) {
int temp;
temp = *a; // save value at address a
*a = *b; // copy value at address b into a
*b = temp; // copy saved value into address b
int main() {
int x, y;
printf("Enter two integers: ");
scanf("%d %d", &x, &y);
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y); // pass addresses of x and y
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
Explanation:
swap receives addresses of x and y.
Inside swap, *a means “value of the variable whose address is in a”.
Swapping *a and *b swaps the original x and y in main.
This is called call by reference using pointers in C.
Problem Title:
“Store and Display Student Details Using Structure and Pointer”
Problem Description:
Write a C program to:
1. Define a structure called Student with the following members:
o roll → integer (roll number of the student)
o name → string (character array, maximum 50 characters)
o marks → float (marks out of 100)
2. In main():
o Declare one variable of type struct Student (for example: s1).
o Declare a pointer to structure (for example: struct Student *ptr;).
o Make the pointer point to the structure variable (ptr = &s1;).
3. Using the pointer:
o Read the student’s roll number, name, and marks from the user.
o Then display the same details using the pointer.
4. Use the -> (arrow) operator to access structure members through the pointer.
Aim / Learning Objectives:
By doing this program, we will learn the following objectives :
How to define and use a struct in C.
How to declare and use a pointer to a structure.
How to access structure members using . (dot) and -> (arrow) operators.
How pointers store addresses and still allow us to read/write data.
#include <stdio.h>
// Define a structure type
struct Student {
int roll;
char name[50];
float marks;
};
int main() {
// Declare a structure variable
struct Student s1;
// Declare a pointer to structure
struct Student *ptr;
// Make pointer point to s1
ptr = &s1;
// Input using pointer (-> operator)
printf("Enter roll number: ");
scanf("%d", &ptr->roll); // same as &[Link]
printf("Enter name: ");
scanf("%49s", ptr->name); // same as [Link] (array name acts as address)
printf("Enter marks: ");
scanf("%f", &ptr->marks); // same as &[Link]
// Output using pointer
printf("\n--- Student Details ---\n");
printf("Roll : %d\n", ptr->roll); // same as [Link]
printf("Name : %s\n", ptr->name); // same as [Link]
printf("Marks : %.2f\n", ptr->marks); // same as [Link]
return 0;
This program shows how to use structures and pointers to structures in C using a
simple Student example.
1. Structure Definition (struct Student)
We create a user-defined type called struct Student which groups three related
pieces of data:
o int roll; → to store the student’s roll number
o char name[50]; → to store the student’s name as a string
o float marks; → to store the student’s marks
A structure lets us keep all these related values together as a single unit
called Student.
2. Creating a Structure Variable and a Pointer
In main, we declare:
o struct Student s1; → one actual student record in memory
o struct Student *ptr; → a pointer that can store the address of a struct
Student variable
Then we do ptr = &s1;, which means ptr now points to s1. From this point
onward, ptr can be used to access and modify the data inside s1.
3. Using the -> (Arrow) Operator with Pointers
Normally, we would access members of s1 using the dot operator, like [Link],
[Link], and [Link].
But since we have a pointer ptr pointing to s1, we use the arrow operator:
o ptr->roll instead of [Link]
o ptr->name instead of [Link]
o ptr->marks instead of [Link]
The arrow -> means: “go to the structure that this pointer points to, then
access this member.”
4. Taking Input Using the Pointer
We take the input from the user using scanf and the pointer:
o scanf("%d", &ptr->roll);
This reads an integer and stores it in the roll member of s1. &ptr->roll is
the address of that member (same as &[Link]).
o scanf("%49s", ptr->name);
Here, ptr->name is a character array. In scanf for strings, the array name is
already treated as an address, so we do not use &. This stores the entered
name inside [Link].
o scanf("%f", &ptr->marks);
This reads the marks and stores them in [Link] via the pointer.
5. Displaying Output Using the Pointer
We print the student data using ptr->roll, ptr->name, and ptr->marks. Even
though we are writing ptr->..., the actual data is stored inside s1, because ptr
points to s1.
o printf("Marks : %.2f\n", ptr->marks); prints the marks with 2 decimal
places.