C++ Pointers Practice Problems
Weekly Technical Blogposts and Bi-Weekly Case Studies
This set contains 10 practice problems on C++ pointers, divided into three categories: Easy, Medium, and Hard. Each problem includes a code solution and an explanation to help you understand how pointers work in C++.
🟢 Easy
1. Basic Pointer Declaration and Dereferencing
Problem:
Declare an integer variable and a pointer to it. Assign a value to the variable and print its value using both the variable and the pointer.
Solution:
#include <iostream>
using namespace std;
int main() {
int a = 5;
int* ptr = &a;
cout << "Value of a: " << a << endl;
cout << "Value using pointer: " << *ptr << endl;
return 0;
}
Explanation:
We use int* ptr = &a to store the address of a in ptr. Using *ptr (dereferencing), we can access the value stored at that address.
2. Pointer Arithmetic
Problem:
Declare an integer array. Use a pointer to traverse and print its elements.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[3] = {10, 20, 30};
int* ptr = arr;
for (int i = 0; i < 3; i++) {
cout << *(ptr + i) << " ";
}
return 0;
}
Explanation:
Pointers can be used to traverse arrays. *(ptr + i) accesses each element just like arr[i].
3. Pointer and Function
Problem:
Write a function that takes a pointer to an int and increments the value. Call this function in main.
Solution:
#include <iostream>
using namespace std;
void increment(int* p) {
(*p)++;
}
int main() {
int a = 7;
increment(&a);
cout << "Value of a after increment: " << a << endl;
return 0;
}
Explanation:
The function increment takes a pointer and modifies the value at that address using (*p)++.
🟡 Medium
4. Swapping Values Using Pointers
Problem:
Write a function to swap two integers using pointers.
Solution:
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 3, y = 4;
swap(&x, &y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}
Explanation:
Passing addresses to the swap function allows it to modify the actual variables using dereferencing.
5. Pointer to Pointer
Problem:
Declare a pointer to a pointer. Assign values appropriately and print the final value.
Solution:
#include <iostream>
using namespace std;
int main() {
int a = 42;
int* p = &a;
int** pp = &p;
cout << "Value using pointer to pointer: " << **pp << endl;
return 0;
}
Explanation:
A pointer to a pointer (int** pp) holds the address of another pointer. **pp accesses the original variable.
6. Dynamic Memory Allocation
Problem:
Dynamically allocate memory for an integer, assign a value to it, and print it. Then free the memory.
Solution:
#include <iostream>
using namespace std;
int main() {
int* ptr = new int;
*ptr = 99;
cout << "Value: " << *ptr << endl;
delete ptr; // Free the memory
return 0;
}
Explanation:new allocates memory, and delete deallocates it. Always free memory to prevent leaks.
7. Array with Dynamic Allocation
Problem:
Dynamically allocate an array of 5 integers. Assign and print values, then deallocate memory.
Solution:
#include <iostream>
using namespace std;
int main() {
int* arr = new int[5];
for (int i = 0; i < 5; ++i)
arr[i] = i * 10;
for (int i = 0; i < 5; ++i)
cout << arr[i] << " ";
delete[] arr; // Free array memory
return 0;
}
Explanation:
Use new[] for arrays and delete[] to deallocate them. Indexing works as with normal arrays.
🔴 Hard
8. Function Returning a Pointer
Problem:
Write a function that returns a pointer to a dynamically allocated integer initialized with a given value.
Solution:
#include <iostream>
using namespace std;
int* createInt(int val) {
int* ptr = new int(val);
return ptr;
}
int main() {
int* p = createInt(123);
cout << "Value: " << *p << endl;
delete p; // Clean up
return 0;
}
Explanation:
Returning a pointer allows dynamic values to live outside the function. Be sure to delete to prevent memory leaks.
9. Pointer and Struct
Problem:
Create a struct and use a pointer to access its members.
Solution:
#include <iostream>
using namespace std;
struct Person {
string name;
int age;
};
int main() {
Person p = {"Alice", 25};
Person* ptr = &p;
cout << ptr->name << " is " << ptr->age << " years old." << endl;
return 0;
}
Explanation:
Use -> to access struct members through a pointer, equivalent to (*ptr).member.
10. Dangling Pointer and Safe Usage
Problem:
Show how a dangling pointer can occur and how to avoid it.
Solution:
#include <iostream>
using namespace std;
int* dangerous() {
int x = 100;
return &x; // Warning: returning address of local variable!
}
int main() {
int* ptr = dangerous();
// cout << *ptr; // Undefined behavior!
// Safe way:
int* safePtr = new int(100);
cout << "Safe value: " << *safePtr << endl;
delete safePtr;
return 0;
}
Explanation:
Returning a pointer to a local variable is dangerous because the variable no longer exists after the function ends. Use dynamic allocation (new) for safe, controlled memory lifetime.


