C Programming • Pointers & File handling
C Programming / Idea of Pointers — Exercises

Idea of Pointers — Exercises

Practical 5 Pointers & File handling

Write a C program to create a pointer to an integer variable and display the value of the variable and its address.

Practical / Solution

Pointer: Idea of Pointers — Exercise 1

Problem

Write a C program to create a pointer to an integer variable and display the value of the variable and its address.

Program

#include <stdio.h> int main() { int number = 25; int *ptr; ptr = &number; printf("Value = %d\n", number); printf("Address = %p\n", (void *)ptr); return 0; }

Explanation

A pointer stores the address of another variable. The address-of operator & obtains the address of number, and that address is stored in ptr.

Expected Output

Value = 25

Address = 0x7ff...

The exact address depends on the system.

Pointer: Idea of Pointers — Exercise 2

Problem

Write a C program to access the value of a variable using a pointer and the dereference operator *.

Program

#include <stdio.h> int main() { int number = 50; int *ptr; ptr = &number; printf("Variable value = %d\n", number); printf("Pointer value = %d\n", *ptr); return 0; }

Explanation

The dereference operator * accesses the value stored at the memory address held by the pointer. Therefore, *ptr gives the value of number.

Expected Output

Variable value = 50

Pointer value = 50

Pointer: Idea of Pointers — Exercise 3

Problem

Write a C program to take an integer from the user and display its value using a pointer.

Program

#include <stdio.h> int main() { int number; int *ptr; printf("Enter a number: "); scanf("%d", &number); ptr = &number; printf("Value using pointer = %d\n", *ptr); return 0; }

Explanation

The pointer ptr stores the address of the input variable. Dereferencing it with *ptr retrieves the variable's value.

Expected Output

Enter a number: 75

Value using pointer = 75

Pointer: Idea of Pointers — Exercise 4

Problem

Write a C program to modify the value of a variable using a pointer.

Program

#include <stdio.h> int main() { int number = 10; int *ptr; ptr = &number; printf("Before modification = %d\n", number); *ptr = 100; printf("After modification = %d\n", number); return 0; }

Explanation

A pointer can be used not only to read a variable but also to modify it. Assigning a new value to *ptr changes the original variable.

Expected Output

Before modification = 10

After modification = 100

Pointer: Idea of Pointers — Exercise 5

Problem

Write a C program to use pointers with two integer variables and calculate their sum.

Program

#include <stdio.h> int main() { int a = 20; int b = 30; int *p1 = &a; int *p2 = &b; int sum; sum = *p1 + *p2; printf("First number = %d\n", *p1); printf("Second number = %d\n", *p2); printf("Sum = %d\n", sum); return 0; }

Explanation

Two pointers are used to access the values of two integer variables. The dereferenced values are then added to calculate the sum.

Expected Output

First number = 20

Second number = 30

Sum = 50

Pointer: Idea of Pointers — Exercise 6

Problem

Write a C program to find the larger of two numbers using pointers.

Program

#include <stdio.h> int main() { int a, b; int *p1, *p2; printf("Enter two numbers: "); scanf("%d %d", &a, &b); p1 = &a; p2 = &b; if (*p1 > *p2) printf("Larger number = %d\n", *p1); else if (*p2 > *p1) printf("Larger number = %d\n", *p2); else printf("Both numbers are equal.\n"); return 0; }

Explanation

The pointers provide access to the original variables, and their dereferenced values are compared using an if-else statement.

Expected Output

Enter two numbers: 45 72

Larger number = 72

Pointer: Idea of Pointers — Exercise 7

Problem

Write a C program to swap two numbers using pointers.

Program

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

Explanation

The addresses of a and b are passed to the function. Because the function receives pointers, it can modify the original variables directly.

Expected Output

Enter two numbers: 10 20

Before swap: a = 10, b = 20

After swap: a = 20, b = 10

Pointer: Idea of Pointers — Exercise 8

Problem

Write a C program to use a pointer with a floating-point variable and display its value.

Program

#include <stdio.h> int main() { float price = 99.50; float *ptr; ptr = &price; printf("Price = %.2f\n", price); printf("Price using pointer = %.2f\n", *ptr); return 0; }

Explanation

Pointers are not limited to integers. A pointer can store the address of a variable of a compatible data type, such as float.

Expected Output

Price = 99.50

Price using pointer = 99.50

Pointer: Idea of Pointers — Exercise 9

Problem

Write a C program to demonstrate that a pointer stores the address of a variable and can be used to access that variable.

Program

#include <stdio.h> int main() { int number = 100; int *ptr = &number; printf("Value of number = %d\n", number); printf("Address of number = %p\n", (void *)&number); printf("Address stored in ptr = %p\n", (void *)ptr); printf("Value using ptr = %d\n", *ptr); return 0; }

Explanation

The address stored in ptr points to the same memory location as the address of number. Dereferencing the pointer gives the value stored at that location.

Expected Output

Value of number = 100

Address of number = 0x7ff...

Address stored in ptr = 0x7ff...

Value using ptr = 100

The exact address depends on the system.

Pointer: Idea of Pointers — Exercise 10

Problem

Write a C program to demonstrate a simple real-world style use of pointers by updating a student's marks through a pointer.

Program

#include <stdio.h> int main() { int marks = 75; int *ptr; ptr = &marks; printf("Original Marks = %d\n", marks); // Update marks through pointer *ptr = *ptr + 10; printf("Updated Marks = %d\n", marks); return 0; }

Explanation

The pointer stores the address of the marks variable. By using *ptr, the program directly updates the original marks. This demonstrates why pointers are useful when a function or another part of a program needs to modify existing data.

Expected Output

Original Marks = 75

Updated Marks = 85