Pointers are one of the most powerful and important concepts in C programming. A pointer is a variable that stores the memory address of another variable instead of storing its actual value.
Pointers are widely used in memory management, arrays, strings, functions, dynamic memory allocation, file handling, data structures (linked lists, stacks, queues, trees, graphs), operating systems, and embedded systems.
Although pointers may seem difficult initially, mastering them is essential for becoming a skilled C programmer. C Pointers practice questions with solutions help to understand the concepts.
In this chapter, you’ll solve practical pointer-based programming questions with complete solutions, sample outputs, explanations, and concepts covered.
1. C Program to Demonstrate Pointer Basics
Problem Statement
Write a C program to declare a pointer, store the address of a variable, and display both the value and its memory address.
C Solution
#include <stdio.h>
int main()
{
int number = 25;
int *ptr;
ptr = &number;
printf("Value of number = %d\n", number);
printf("Address of number = %p\n", &number);
printf("Pointer stores = %p\n", ptr);
printf("Value using pointer = %d", *ptr);
return 0;
}
Sample Output
Value of number = 25
Address of number = 0x61ff08
Pointer stores = 0x61ff08
Value using pointer = 25
Note: The memory address will be different on every computer and every program execution.
Explanation
&numberreturns the memory address of the variable.ptrstores that address.*ptraccesses the value stored at that address.
Concepts Covered
- Pointer Declaration
- Address Operator (
&) - Dereference Operator (
*) - Memory Address
2. C Program to Add Two Numbers Using Pointers
Problem Statement
Write a C program to add two numbers using pointers.
C Solution
#include <stdio.h>
int main()
{
int first, second;
int *ptr1, *ptr2;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
ptr1 = &first;
ptr2 = &second;
printf("Sum = %d", *ptr1 + *ptr2);
return 0;
}
Sample Output
Enter first number: 20
Enter second number: 35
Sum = 55
Explanation
The pointers store the addresses of the two variables. Using the dereference operator (*), the actual values are accessed and added.
Concepts Covered
- Pointers
- Dereferencing
- Arithmetic Operations
3. C Program to Swap Two Numbers Using Pointers
Problem Statement
Write a C program to swap two numbers using pointers.
C Solution
#include <stdio.h>
int main()
{
int first, second;
int *ptr1, *ptr2;
int temp;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
ptr1 = &first;
ptr2 = &second;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
printf("After Swapping:\n");
printf("First Number = %d\n", first);
printf("Second Number = %d", second);
return 0;
}
Sample Output
Enter first number: 15
Enter second number: 40
After Swapping:
First Number = 40
Second Number = 15
Explanation
The pointers access the original variables directly, allowing their values to be exchanged using a temporary variable.
Concepts Covered
- Pointers
- Swapping Variables
- Dereferencing
- Memory Access
4. C Program to Find the Largest Number Using Pointers
Problem Statement
Write a C program to find the larger of two numbers using pointers.
C Solution
#include <stdio.h>
int main()
{
int first, second;
int *ptr1, *ptr2;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
ptr1 = &first;
ptr2 = &second;
if(*ptr1 > *ptr2)
{
printf("Largest Number = %d", *ptr1);
}
else
{
printf("Largest Number = %d", *ptr2);
}
return 0;
}
Sample Output
Enter first number: 65
Enter second number: 28
Largest Number = 65
Explanation
The pointers store the addresses of both variables. By dereferencing the pointers using *, the program compares the actual values and prints the larger number.
Concepts Covered
- Pointers
- Dereferencing
- Comparison Operators
- if-else Statement
5. C Program to Calculate the Sum of Array Elements Using Pointers
Problem Statement
Write a C program to calculate the sum of all elements in an array using pointers.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int *ptr;
int i, sum = 0;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
ptr = numbers;
for(i = 0; i < 5; i++)
{
sum = sum + *(ptr + i);
}
printf("Sum = %d", sum);
return 0;
}
Sample Output
Enter 5 numbers:
10
20
30
40
50
Sum = 150
Explanation
The array name acts as a pointer to its first element.
The expression:
*(ptr + i)
accesses each array element using pointer arithmetic instead of array indexing.
Concepts Covered
- Pointers
- Arrays
- Pointer Arithmetic
- Dereferencing
6. C Program to Print Array Elements Using Pointer Arithmetic
Problem Statement
Write a C program to print all elements of an array using pointer arithmetic instead of array indexing.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int *ptr;
int i;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
ptr = numbers;
printf("Array Elements:\n");
for(i = 0; i < 5; i++)
{
printf("%d ", *(ptr + i));
}
return 0;
}
Sample Output
Enter 5 numbers:
11
22
33
44
55
Array Elements:
11 22 33 44 55
Explanation
Instead of using numbers[i], the program accesses each element using pointer arithmetic.
*(ptr + i)
This expression points to the current array element and retrieves its value.
Concepts Covered
- Pointer Arithmetic
- Arrays
- Dereferencing
- Memory Access
7. C Program to Reverse an Array Using Pointers
Problem Statement
Write a C program to display an array in reverse order using pointers.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int *ptr;
int i;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
ptr = &numbers[4];
printf("Reversed Array:\n");
for(i = 0; i < 5; i++)
{
printf("%d ", *(ptr - i));
}
return 0;
}
Sample Output
Enter 5 numbers:
10
20
30
40
50
Reversed Array:
50 40 30 20 10
Explanation
The pointer initially points to the last array element.
The expression:
*(ptr - i)
moves backward through the array while printing each element.
Concepts Covered
- Pointer Arithmetic
- Reverse Traversal
- Arrays
- Dereferencing
8. C Program to Copy One Array into Another Using Pointers
Problem Statement
Write a C program to copy all elements from one array into another using pointers.
C Solution
#include <stdio.h>
int main()
{
int source[5], destination[5];
int *ptr1, *ptr2;
int i;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &source[i]);
}
ptr1 = source;
ptr2 = destination;
for(i = 0; i < 5; i++)
{
*(ptr2 + i) = *(ptr1 + i);
}
printf("Copied Array:\n");
for(i = 0; i < 5; i++)
{
printf("%d ", destination[i]);
}
return 0;
}
Sample Output
Enter 5 numbers:
2
4
6
8
10
Copied Array:
2 4 6 8 10
Explanation
Both pointers traverse their respective arrays simultaneously. Each element from the source array is copied into the destination array.
Concepts Covered
- Arrays
- Pointer Arithmetic
- Array Copying
- Dereferencing
9. C Program to Find the Length of a String Using Pointers
Problem Statement
Write a C program to calculate the length of a string using pointers.
C Solution
#include <stdio.h>
int main()
{
char text[100];
char *ptr;
int length = 0;
printf("Enter a string: ");
scanf("%s", text);
ptr = text;
while(*ptr != '\0')
{
length++;
ptr++;
}
printf("Length = %d", length);
return 0;
}
Sample Output
Enter a string: Pointer
Length = 7
Explanation
The pointer moves one character at a time until it reaches the null character ('\0').
Each movement increases the length counter.
Concepts Covered
- String Pointers
- Pointer Increment
- Null Character
- Character Traversal
10. C Program to Copy a String Using Pointers
Problem Statement
Write a C program to copy one string into another using pointers.
C Solution
#include <stdio.h>
int main()
{
char source[100], destination[100];
char *ptr1, *ptr2;
printf("Enter a string: ");
scanf("%s", source);
ptr1 = source;
ptr2 = destination;
while(*ptr1 != '\0')
{
*ptr2 = *ptr1;
ptr1++;
ptr2++;
}
*ptr2 = '\0';
printf("Copied String: %s", destination);
return 0;
}
Sample Output
Enter a string: Programming
Copied String: Programming
Explanation
Both pointers move together through the source and destination strings until the null character is reached. Finally, the destination string is terminated with '\0'.
Concepts Covered
- String Pointers
- Pointer Traversal
- String Copy
- Dereferencing
11. C Program to Swap Two Strings Using Pointers
Problem Statement
Write a C program to swap two strings using pointers.
C Solution
#include <stdio.h>
#include <string.h>
int main()
{
char first[100], second[100], temp[100];
char *ptr1, *ptr2;
printf("Enter first string: ");
scanf("%s", first);
printf("Enter second string: ");
scanf("%s", second);
ptr1 = first;
ptr2 = second;
strcpy(temp, ptr1);
strcpy(ptr1, ptr2);
strcpy(ptr2, temp);
printf("\nAfter Swapping:\n");
printf("First String = %s\n", first);
printf("Second String = %s", second);
return 0;
}
Sample Output
Enter first string: Apple
Enter second string: Orange
After Swapping:
First String = Orange
Second String = Apple
Explanation
Pointers reference both strings. A temporary string is used to exchange their contents safely.
Concepts Covered
- String Pointers
- String Swapping
- Character Arrays
- Pointer Variables
12. C Program to Compare Two Strings Using Pointers
Problem Statement
Write a C program to compare two strings using pointers without using strcmp().
C Solution
#include <stdio.h>
int main()
{
char first[100], second[100];
char *ptr1, *ptr2;
printf("Enter first string: ");
scanf("%s", first);
printf("Enter second string: ");
scanf("%s", second);
ptr1 = first;
ptr2 = second;
while(*ptr1 != '\0' && *ptr2 != '\0')
{
if(*ptr1 != *ptr2)
break;
ptr1++;
ptr2++;
}
if(*ptr1 == *ptr2)
printf("Strings are Equal.");
else
printf("Strings are Not Equal.");
return 0;
}
Sample Output
Enter first string: Coding
Enter second string: Coding
Strings are Equal.
Explanation
The pointers move through both strings simultaneously. If any pair of characters differs, the strings are considered different.
Concepts Covered
- Pointer Traversal
- String Comparison
- Dereferencing
- Character Processing
13. C Program to Count Vowels in a String Using Pointers
Problem Statement
Write a C program to count the total number of vowels in a string using pointers.
C Solution
#include <stdio.h>
int main()
{
char text[100];
char *ptr;
int vowels = 0;
printf("Enter a string: ");
scanf("%s", text);
ptr = text;
while(*ptr != '\0')
{
if(*ptr=='A'||*ptr=='E'||*ptr=='I'||*ptr=='O'||*ptr=='U'||
*ptr=='a'||*ptr=='e'||*ptr=='i'||*ptr=='o'||*ptr=='u')
{
vowels++;
}
ptr++;
}
printf("Total Vowels = %d", vowels);
return 0;
}
Sample Output
Enter a string: Education
Total Vowels = 5
Explanation
The pointer visits each character one by one and checks whether it is a vowel.
Concepts Covered
- String Pointers
- Pointer Increment
- Character Comparison
- String Processing
14. C Program to Reverse a String Using Pointers
Problem Statement
Write a C program to reverse a string using pointers.
C Solution
#include <stdio.h>
int main()
{
char text[100];
char *start, *end, temp;
printf("Enter a string: ");
scanf("%s", text);
start = text;
end = text;
while(*end != '\0')
{
end++;
}
end--;
while(start < end)
{
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
printf("Reversed String: %s", text);
return 0;
}
Sample Output
Enter a string: Pointer
Reversed String: retnioP
Explanation
Two pointers are used:
startpoints to the beginning.endpoints to the last character.
The characters are swapped until both pointers meet.
Concepts Covered
- Pointer Arithmetic
- Two-Pointer Technique
- String Reversal
- Character Swapping
15. C Program to Find the Largest Element of an Array Using Pointers
Problem Statement
Write a C program to find the largest element in an array using pointers.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int *ptr;
int i, largest;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
ptr = numbers;
largest = *ptr;
for(i = 1; i < 5; i++)
{
if(*(ptr + i) > largest)
{
largest = *(ptr + i);
}
}
printf("Largest Element = %d", largest);
return 0;
}
Sample Output
Enter 5 numbers:
45
12
87
39
60
Largest Element = 87
Explanation
The pointer traverses the array using pointer arithmetic and keeps track of the largest value.
Concepts Covered
- Pointer Arithmetic
- Arrays
- Searching
- Dereferencing
Chapter Summary
In this chapter, you learned how pointers work in C programming and how they interact with variables, arrays, strings, and functions. You practiced storing addresses, dereferencing pointers, performing pointer arithmetic, traversing arrays, manipulating strings, swapping values, copying data, and solving practical pointer-based problems. Pointers are one of the most powerful features of C and form the foundation for advanced topics such as dynamic memory allocation, linked lists, stacks, queues, trees, and file handling.
Key Takeaways
- A pointer stores the memory address of another variable.
- The
&operator returns the address of a variable. - The
*operator accesses the value stored at a memory address. - Arrays and pointers are closely related in C.
- Pointer arithmetic allows efficient traversal of arrays and strings.
- Pointers make swapping variables and modifying data more efficient.
- String manipulation becomes easier using pointer traversal.
- Pointers are essential for dynamic memory allocation and data structures.
- Understanding pointers is critical for writing optimized C programs.
- Pointers are one of the most frequently asked topics in C programming interviews.
Frequently Asked Questions (FAQs)
1. What is a pointer in C?
A pointer is a variable that stores the memory address of another variable.
2. What is the difference between & and *?
&returns the address of a variable.*accesses the value stored at a memory address.
3. Why are pointers important in C?
Pointers enable efficient memory access, dynamic memory allocation, function parameter passing, and implementation of advanced data structures.
4. What is pointer arithmetic?
Pointer arithmetic allows moving through memory locations using operations such as ptr + 1 or ptr - 1.
5. How are arrays related to pointers?
The name of an array represents the address of its first element, allowing arrays to be accessed using pointer arithmetic.
6. Can pointers be used with strings?
Yes. Since strings are character arrays, pointers can efficiently traverse and manipulate strings.
7. What is dereferencing?
Dereferencing means accessing the value stored at the memory address contained in a pointer using the * operator.
8. Where are pointers used in real-world programming?
Pointers are widely used in operating systems, embedded systems, compilers, databases, graphics engines, networking applications, memory management, linked lists, stacks, queues, trees, graphs, and dynamic data structures. They are one of the most powerful and essential features of C programming.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
