Pointers are an important part of C++. They allow a program to work directly with memory addresses. Pointers may look confusing at first, but the basic idea is simple. A normal variable stores a value, while a pointer stores the address of another variable. Let’s start understanding C++ Pointers.
What is a Pointer?
Suppose we have:
int age = 14;
The variable age stores the value 14.
Every variable is stored somewhere in the computer’s memory. A pointer can store the memory address of that variable.
Think of it like this:
age → 14
pointer → address of age
Creating a Pointer
A pointer is declared using *.
int* ptr;
Here, ptr is a pointer that can store the address of an int variable.
To store the address of a variable, use the & operator.
int age = 14;
int* ptr = &age;
Here:
agestores14.&agegets the address ofage.ptrstores that address.
Getting the Address of a Variable
The & operator is called the address-of operator.
Example:
#include <iostream>
using namespace std;
int main() {
int age = 14;
cout << &age;
return 0;
}
The output will be a memory address, which may look something like:
0x7ffca1234567
The exact address will be different each time the program runs.
Accessing a Value Using a Pointer
The * operator can also be used to get the value stored at the address held by a pointer.
This is called dereferencing.
#include <iostream>
using namespace std;
int main() {
int age = 14;
int* ptr = &age;
cout << *ptr;
return 0;
}
Output:
14
Here, ptr stores the address of age, and *ptr gives us the value stored at that address.
Changing a Value Using a Pointer
A pointer can also be used to change the value of a variable.
#include <iostream>
using namespace std;
int main() {
int age = 14;
int* ptr = &age;
*ptr = 15;
cout << age;
return 0;
}
Output:
15
Changing *ptr changes the value of age because both refer to the same memory location.
Pointer Example
Let’s look at the address, pointer, and value together.
#include <iostream>
using namespace std;
int main() {
int number = 25;
int* ptr = &number;
cout << "Value: " << number << "\n";
cout << "Address: " << &number << "\n";
cout << "Pointer: " << ptr << "\n";
cout << "Value using pointer: " << *ptr;
return 0;
}
The output will look similar to:
Value: 25
Address: 0x7ffca1234567
Pointer: 0x7ffca1234567
Value using pointer: 25
The exact memory address will be different on different systems and program runs.
Pointers and Arrays
Pointers are closely related to arrays.
For example:
#include <iostream>
using namespace std;
int main() {
int numbers[3] = {10, 20, 30};
int* ptr = numbers;
cout << *ptr;
return 0;
}
Output:
10
Here, ptr points to the first element of the array.
Pointers become especially useful when working with arrays, functions, and dynamic memory.
Null Pointer
A pointer that does not point to a valid object can be set to nullptr.
#include <iostream>
using namespace std;
int main() {
int* ptr = nullptr;
cout << "Pointer is not pointing to a value.";
return 0;
}
nullptr is safer and clearer than leaving a pointer uninitialized.
A Simple Example
Let’s use a pointer to change a number.
#include <iostream>
using namespace std;
int main() {
int number = 10;
int* ptr = &number;
cout << "Before: " << number << "\n";
*ptr = 50;
cout << "After: " << number;
return 0;
}
Output:
Before: 10
After: 50
The pointer points to number, so changing *ptr changes the original variable.
Key Points to Remember
- A pointer stores the address of another variable.
&gets the address of a variable.*is used to access the value at an address.- Pointers can be used to change the original value.
- Pointers are closely related to arrays and memory management.
nullptrrepresents a pointer that is not pointing to an object.
Mini Quiz
Mini Project
Mini Project: Number Modifier Using a Pointer
Question:
Write a C++ program that uses a pointer to modify the value of a variable.
- Store a number in an
intvariable. - Create a pointer that stores the address of the variable.
- Display the original value.
- Use the pointer to change the value.
- Display the updated value.
- Use
coutto display the results.
View Answer Code
#include <iostream>
using namespace std;
int main() {
int number = 10;
int* ptr = &number;
cout << "Before: " << number << "\n";
*ptr = 50;
cout << "After: " << number;
return 0;
}
What’s Next?
In the next chapter, we will learn about C++ Structures and see another simple way to create an alias for an existing variable.
Frequently Asked Questions (FAQs)
Q1. What are C++ Pointers?
C++ Pointers are variables that store the memory address of another variable. They allow programs to work directly with memory locations.
Q2. How do you declare a C++ pointer?
A C++ pointer declaration uses * with a data type and pointer name. For example, int* ptr; declares a pointer that can store the address of an integer.
Q3. What is a C++ memory address?
A C++ memory address identifies the location where a variable is stored in computer memory. The & operator can be used to obtain a variable’s address.
Q4. What is C++ dereferencing?
C++ dereferencing means accessing the value stored at the memory address held by a pointer. The * operator is used for dereferencing.
Q5. Where are C++ pointers commonly used?
C++ Pointers are commonly used with arrays, functions, dynamic memory, data structures, and situations where direct memory access is required.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
