C++ Pointers
In this tutorial, we will learn about pointers in C++ and their working with the help of examples.
Pointers
- Pointers are variables that store the memory addresses of other variables.
- Pointers are symbolic representation of address.
- Pointers are one of the best features of c++ in comparison to other proggramming languages like- java, python etc.
- They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures.
- A pointer holds the address of an object stored in memory. The pointer then simply “points” to the object. The type of the object must correspond with the type of the pointer.
Address in C++
If we have a variable var in our program, &var will give us its address in the memory.
Example 1: C++ Display the Variable Addresses
#include <iostream>
using namespace std;
int main(){
// declare variables
int var1 = 2;
int var2 = 4;
// display the address of var1
cout << "The Address of var1: "<< &var1 << endl;
// display the address of var2
cout << "The Address of var2: " << &var2 << endl;
}
Output
The Address of var1: 0x7ffdd6999d20 The Address of var2: 0x7ffdd6999d24
Working
- Here,
0x
at the beginning represents the address is in the hexadecimal form. - Notice that the first address differs from the second by 4 bytes.
- This is because the size of an
int
variable is 4 bytes in a 64-bit system.
Note: You may not get the same results when you run the program.
How to declare a Pointer ?
As mentioned above, pointers are used to store addresses rather than values.
Syntax:
data_type *pointer_name;
Here is how we can declare pointers.
int *pVar;
Here, we have declared a pointer pVar of the int
type.
Rules for declaring a pointer
- The data type is the base type of the pointer which must be a valid C++ data type.
- The variable_name is should be the name of the pointer variable.
- Asterisk * used above for pointer declaration is similar to asterisk used to perform multiplication operation. it is the asterisk that marks the variable as a pointer.
We can also declare pointers in the following way.
int* pVar; // preferred syntax
Let's take another example of declaring pointers.
int* pVar, p;
Here, we have declared a pointer pVar and a normal variable p.
Note: The *
operator is used after the data type to declare pointers.
Assigning Addresses to Pointers
Here is how we can assign addresses to pointers:
int* pVar, a;
a = 2;
// assign address of a to pVar pointer
pVar = &a;
Here, 2
is assigned to the variable a. And, the address of a is assigned to the pVar pointer with the code pVar = &a
.
When &
is used with pointers, it's called the reference operator. It returns the variable's address.
Get the Value from the Address Using Pointers
To get the value pointed by a pointer, we use the *
operator. For example:
int* pVar, a;
a = 2;
// assign address of a to pVar
pVar = &a;
// access value pointed by pointVar
cout << *pVar << endl; // Output: 2
In the above code, the address of a is assigned to pVar. We have used the *pVar
to get the value stored in that address.
When *
is used with pointers, it's called the dereference operator. It operates on a pointer and gives the value pointed by the address stored in the pointer. That is, *pVar = a
.
Note: In C++, pVar and *pVar is completely different. We cannot do something like *pVar = &a;
Example 2: Working of C++ Pointers
#include <iostream>
using namespace std;
int main() {
int a = 2;
int* pVar; // declare pointer variable
pVar = &a; // store address of a
cout << "a = " << a << endl; // print value of a
cout << "Address of a (&a) = " << &a << endl; // print address of a
cout << "pVar = " << pVar << endl; // print pointer pVar
// print the content of the address pVar points to
cout << "The content of the address pointed to by pVar (*pVar) = " << *pVar << endl;
return 0;
}
Output
a = 2 Address of a (&a) = 0x7ffc3d579a0c pVar = 0x7ffc3d579a0c The content of the address pointed to by pVar (*pVar) = 2
Changing Value Pointed by Pointers
If pVar points to the address of a, we can change the value of a by using *pVar.
For example,
int a = 2;
int* pVar;
pVar = &a; // assign address of a
*pVar = 4; // change value at address pVar
cout << a << endl; // Output: 4
Here, pVar and &a
have the same address, the value of a will also be changed when *pVar is changed.
Example 3: Program to display Changing Value Pointed by Pointers
#include <iostream>
using namespace std;
int main() {
int a = 2;
int* pVar;
pVar = &a; // store address of a
// print a
cout << "a = " << a << endl;
// print *pVar
cout << "*pVar = " << *pVar << endl;
cout << "Changing value of a to 4:" << endl;
a = 4; // change value of a to 4
// print a
cout << "a = " << a << endl;
// print *pVar
cout << "*pVar = " << *pVar << endl;
cout << "Changing value of *pVar to 6:" << endl;
// change value of a to 6
*pVar = 6;
// print a
cout << "a = " << a << endl;
// print *pointVar
cout << "*pVar = " << *pVar << endl;
return 0;
}
Output
a = 2 *pVar = 2 Changing value of a to 4: a = 4 *pVar = 4 Changing value of *pVar to 6: a = 6 *pVar = 6
Common Errors when using pointers
Suppose, we want a pointer pVar to point to the address of a. Then,
int a, *pVar;
// Wrong!
// pVar is an address but a is not
pVar = a;
// Wrong!
// &a is an address
// *pVar is the value stored in &a
*pVar = &a;
// Correct!
// pVar is an address and so is &a
pVar = &a;
// Correct!
// both *pVar and a are values
*pVar = a;
Advantages of Pointers
- The pointer allows you to access any memory location.
- It reduces the code and improves the performance.
- It used to retrieve the strings, trees and is used with arrays, structures and functions.
- By using pointers, you can return multiple values form function.
Recommended Readings to Use Pointers in C++:
- How to use generic data type pointers using a void pointer?
- How to represent an array using a pointer?
- How to use pointers with functions?
- How to use pointers with structures?
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Pointers in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Void Pointers
.