Pointers are one of the most powerful and important concepts in C++ programming. A pointer is a special variable that stores the memory address of another variable instead of storing the actual value.
Pointers are widely used in:
- Dynamic Memory Allocation
- Data Structures (Linked List, Tree, Graph)
- Function Parameters
- File Handling
- Object-Oriented Programming
- System Programming
- Competitive Programming
Understanding pointers is essential for becoming an advanced C++ programmer because many real-world applications rely on efficient memory management. C++ Pointers Practice Questions with Solutions help to understand the concepts.
Basic Pointer Syntax
int number = 10;
int *ptr = &number;
Here:
numberstores the value 10&numbergives the memory address ofnumberptrstores that memory address*ptraccesses the value stored at that address
In this chapter, you’ll solve practical pointer-based problems to understand memory addresses, dereferencing, pointer arithmetic, pointer functions, and arrays with pointers.
Each question includes:
- Problem Statement
- Complete C++ Solution
- Sample Input
- Sample Output
- Explanation
- Concepts Covered
Let’s begin with the first five pointer practice questions.
1. C++ Program to Display the Address of a Variable Using a Pointer
Problem Statement
Write a C++ program to display the memory address of a variable using a pointer.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int number = 25;
int *ptr = &number;
cout << "Value = "
<< number << endl;
cout << "Address = "
<< ptr;
return 0;
}
Sample Output
Value = 25
Address = 0x61ff08
Note: The memory address will be different on every computer.
Explanation
The pointer stores the address of the variable using the address-of (&) operator.
Concepts Covered
- Pointer Variable
- Address Operator (
&) - Memory Address
2. C++ Program to Access the Value Using a Pointer
Problem Statement
Write a C++ program to access the value of a variable using a pointer.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int number = 50;
int *ptr = &number;
cout << "Value using Pointer = "
<< *ptr;
return 0;
}
Sample Output
Value using Pointer = 50
Explanation
The dereference operator (*) retrieves the value stored at the memory address pointed to by the pointer.
Concepts Covered
- Dereference Operator (
*) - Pointer Access
- Memory Value
3. C++ Program to Modify a Variable Using a Pointer
Problem Statement
Write a C++ program to modify the value of a variable through a pointer.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int number = 15;
int *ptr = &number;
*ptr = 100;
cout << "Updated Value = "
<< number;
return 0;
}
Sample Output
Updated Value = 100
Explanation
Changing the value using *ptr also changes the original variable because both refer to the same memory location.
Concepts Covered
- Dereferencing
- Pointer Assignment
- Memory Manipulation
4. C++ Program to Swap Two Numbers Using Pointers
Problem Statement
Write a C++ program to swap two numbers using pointers.
C++ Solution
#include <iostream>
using namespace std;
void swapNumbers(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int num1 = 10;
int num2 = 20;
swapNumbers(&num1, &num2);
cout << "First Number = "
<< num1 << endl;
cout << "Second Number = "
<< num2;
return 0;
}
Sample Output
First Number = 20
Second Number = 10
Explanation
The function receives the memory addresses of both variables and swaps their values directly.
Concepts Covered
- Function with Pointers
- Call by Address
- Swapping
5. C++ Program to Add Two Numbers Using Pointers
Problem Statement
Write a C++ program to calculate the sum of two numbers using pointers.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int first, second;
cout << "Enter first number: ";
cin >> first;
cout << "Enter second number: ";
cin >> second;
int *ptr1 = &first;
int *ptr2 = &second;
int sum = *ptr1 + *ptr2;
cout << "Sum = "
<< sum;
return 0;
}
Sample Input
Enter first number: 45
Enter second number: 35
Sample Output
Sum = 80
Explanation
The values are accessed through pointers using the dereference operator and then added together.
Concepts Covered
- Multiple Pointers
- Pointer Arithmetic (Basic)
- Dereferencing
- Mathematical Operations
6. C++ Program to Demonstrate Pointer Arithmetic
Problem Statement
Write a C++ program to demonstrate pointer arithmetic by moving a pointer to the next element of an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int *ptr = numbers;
cout << "First Element = " << *ptr << endl;
ptr++;
cout << "Second Element = " << *ptr << endl;
ptr++;
cout << "Third Element = " << *ptr;
return 0;
}
Sample Output
First Element = 10
Second Element = 20
Third Element = 30
Explanation
Each time the pointer is incremented using ptr++, it points to the next integer in the array.
Concepts Covered
- Pointer Arithmetic
- Array Pointer
- Increment Operator
7. C++ Program to Traverse an Array Using Pointers
Problem Statement
Write a C++ program to display all elements of an array using pointers.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {5, 10, 15, 20, 25};
int *ptr = numbers;
cout << "Array Elements:\n";
for (int i = 0; i < 5; i++)
{
cout << *(ptr + i) << " ";
}
return 0;
}
Sample Output
Array Elements:
5 10 15 20 25
Explanation
Instead of using array indexing (numbers[i]), the program accesses elements using pointer arithmetic (*(ptr + i)).
Concepts Covered
- Array Traversal
- Pointer Arithmetic
- Dereference Operator
8. C++ Program to Find the Largest Element Using Pointers
Problem Statement
Write a C++ program to find the largest element in an array using pointers.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {45, 18, 92, 67, 31};
int *ptr = numbers;
int largest = *ptr;
for (int i = 1; i < 5; i++)
{
if (*(ptr + i) > largest)
{
largest = *(ptr + i);
}
}
cout << "Largest Element = "
<< largest;
return 0;
}
Sample Output
Largest Element = 92
Explanation
The pointer traverses the array while comparing each element to find the maximum value.
Concepts Covered
- Pointer Traversal
- Maximum Element
- Pointer Arithmetic
9. 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 <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int *ptr = numbers + 4;
cout << "Reversed Array:\n";
for (int i = 4; i >= 0; i--)
{
cout << *ptr << " ";
ptr--;
}
return 0;
}
Sample Output
Reversed Array:
50 40 30 20 10
Explanation
The pointer starts from the last element and moves backward until the first element is reached.
Concepts Covered
- Reverse Traversal
- Pointer Decrement
- Array Pointer
10. C++ Program to Demonstrate Pointer to Pointer
Problem Statement
Write a C++ program to demonstrate the use of a pointer to a pointer.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int number = 100;
int *ptr = &number;
int **doublePtr = &ptr;
cout << "Value = " << number << endl;
cout << "Using Pointer = " << *ptr << endl;
cout << "Using Pointer to Pointer = " << **doublePtr;
return 0;
}
Sample Output
Value = 100
Using Pointer = 100
Using Pointer to Pointer = 100
Explanation
ptrstores the address ofnumber.doublePtrstores the address ofptr.**doublePtraccesses the original value stored innumber.
Concepts Covered
- Pointer to Pointer
- Double Dereferencing
- Memory Addresses
11. C++ Program to Count 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 <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int *ptr = numbers;
int sum = 0;
for (int i = 0; i < 5; i++)
{
sum += *(ptr + i);
}
cout << "Sum = "
<< sum;
return 0;
}
Sample Output
Sum = 150
Explanation
The pointer traverses the array and accesses every element using pointer arithmetic.
Concepts Covered
- Pointer Traversal
- Array Sum
- Pointer Arithmetic
12. C++ Program to Copy One Variable to Another Using Pointers
Problem Statement
Write a C++ program to copy the value of one variable to another using pointers.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int first = 75;
int second = 0;
int *ptr1 = &first;
int *ptr2 = &second;
*ptr2 = *ptr1;
cout << "First Number = "
<< first << endl;
cout << "Second Number = "
<< second;
return 0;
}
Sample Output
First Number = 75
Second Number = 75
Explanation
The value stored at the memory location pointed to by ptr1 is copied into the location pointed to by ptr2.
Concepts Covered
- Multiple Pointers
- Dereferencing
- Assignment Through Pointer
13. C++ Program to Compare Two Pointers
Problem Statement
Write a C++ program to compare whether two pointers point to the same memory location.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int number = 100;
int *ptr1 = &number;
int *ptr2 = &number;
if (ptr1 == ptr2)
{
cout << "Both pointers point to the same address.";
}
else
{
cout << "Pointers point to different addresses.";
}
return 0;
}
Sample Output
Both pointers point to the same address.
Explanation
Both pointers store the address of the same variable, so the comparison returns true.
Concepts Covered
- Pointer Comparison
- Memory Address
- Equality Operator
14. C++ Program to Find the Length of a String Using a Pointer
Problem Statement
Write a C++ program to find the length of a string using pointers.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
char text[] = "Programming";
char *ptr = text;
int length = 0;
while (*ptr != '\0')
{
length++;
ptr++;
}
cout << "Length = "
<< length;
return 0;
}
Sample Output
Length = 11
Explanation
The pointer moves through each character until it reaches the null character ('\0'), counting the total characters.
Concepts Covered
- Character Pointer
- Null Character
- Pointer Increment
15. C++ Program to Print Array Elements Using Only Pointers
Problem Statement
Write a C++ program to print all array elements without using array indexing.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {11, 22, 33, 44, 55};
int *ptr = numbers;
cout << "Array Elements:\n";
while (ptr < numbers + 5)
{
cout << *ptr << " ";
ptr++;
}
return 0;
}
Sample Output
Array Elements:
11 22 33 44 55
Explanation
Instead of using array indexes, the pointer moves from the first element to the last element and prints each value.
Concepts Covered
- Pointer Traversal
- While Loop
- Array Without Indexing
Chapter Summary
In this chapter, you learned the fundamentals of pointers in C++. You practiced storing memory addresses, dereferencing pointers, modifying variables through pointers, swapping values, performing pointer arithmetic, traversing arrays, using pointer-to-pointer, comparing pointers, and processing strings using character pointers. Pointers are one of the most important concepts in C++ because they provide direct access to memory and are essential for dynamic memory allocation, data structures, and object-oriented programming.
Key Takeaways
- A pointer stores the memory address of another variable.
- The
&operator returns the memory address of a variable. - The
*operator is used to declare pointers and dereference them. - Pointer arithmetic allows movement between array elements.
- Arrays and pointers are closely related in C++.
- Functions can modify variables directly using pointers.
- Pointer-to-pointer stores the address of another pointer.
- Character pointers are commonly used for C-style strings.
- Pointers are heavily used in data structures and system programming.
- Strong pointer knowledge is essential before learning dynamic memory allocation and linked lists.
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 the pointer’s address.
3. What is pointer arithmetic?
Pointer arithmetic allows a pointer to move to adjacent memory locations using operators like ++, --, +, and -.
4. Why are pointers important in C++?
Pointers enable efficient memory management, dynamic memory allocation, and implementation of advanced data structures.
5. What is a pointer to a pointer?
A pointer to a pointer stores the address of another pointer and is accessed using the ** operator.
6. Can pointers be used with arrays?
Yes. The name of an array acts as a pointer to its first element, allowing array traversal using pointer arithmetic.
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 commonly used?
Pointers are widely used in linked lists, trees, graphs, dynamic memory allocation, file handling, operating systems, game engines, and high-performance software development.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
