Just like C language, C++ has also provided us a very important feature known as - Pointers.
Pointers allows us to access and point to a particular address of a memory location. By using the address of a memory location,
we could access the original value stored at that particular address.
In order to understand the concept of pointers, let's see a line of code -
int a = 10;
This line of code tells the compiler to reserve a space in the memory location, store an int value in it i.e. 10 and
name this space in the memory location, a.
This memory location where the int value i.e. 10 is stored is also referred to as a memory address.
Let's suppose this memory address is 0x28ff28(hexadecimal form of memory address found by computer automatically where value, 10, is to be stored). Look at the diagram below for reference.
Pointers allows us to access the original value stored at that particular address. We could pass this address to a function that takes pointer argument
and allow it to make permanent changes to the value at the memory address.
In order to do this, we will have to use two operators -
&"address of" operator, which gives us the memory address of a value.
*"value at address" operator, which gives us the value at an address.
An example of Pointer
In the upcoming code example, we are going to create a pointer variable which points to a memory address, containing an int value 10.
#include<iostream>
using namespace std;
int main()
{
int a = 10;
//Declaring a pointer variable b, which points to the address of int variable a
int *b = &a;
cout<< "The value of a is : " << a << "\n";
cout<< "The address of a is : " << &a << "\n";
cout<< "The value at address pointed by pointer b is : " << *b << "\n";
cout<< "The address pointed by pointer variable b is : " << b;
return 0;
}
Output
The value of a is : 10
The address of a is : 0x28ff28
The value at address pointed by pointer b is : 10
The address pointed by pointer variable b is : 0x28ff28
Program analysis
We have declared a pointer variable, b which is pointing to address of int variable a.
We have successfully accessed the address of int variable a
using &"address of" operator i.e. &a. The memory address is printed using %u format specifier.
Next, have accessed the int value(10) stored in a by
using *"value at address" operator with pointer variable, b i.e. *b.
Finally, we have printed the address pointed by the pointer variable, b.
Advertisement
Using pointer to modify a value passed to a function by reference
In the upcoming example, we are going to create a function named subtract10 which subtracts 10 from value of the variable of int type passed to it using pointers, when this function is called.
// Calling a function with argument, by reference
#include<iostream>
using namespace std;
int main()
{
// function prototype declaration
void subtract10(int *);
int a = 10;
cout<< "The value in a is : " << a << "\n";
// calling the subtract10 function
subtract10(&a);
cout<< "After the subtract10 function is called " << "\n";
cout<< "The value in a is : " << a << "\n";
return 0;
}
// function to subtract 10 from the int value passed to it
void subtract10(int *i)
{
*i = *i - 10;
}
Output-
The value in a is : 10
After the subtract10 function is called
The value in a is : 0
Program Analysis
Declaring function prototype -
In the above mentioned example, we have created a function named subtract10 and have declared its prototype as -
/* function prototype declaration*/
void subtract10(int *);
The prototype of this function is declared with a voidreturn type, because this method will not return any value when it is called.
This function will be passed the address of an int value when it is called.
Hence we have specified a pointer that points to a value of int type i.e. int *, within the parenthesis().
Calling the function - We have called the subtract10 method by passing the address of value of an int
variable named a as an argument, by using the "address of" operator i.e. &.
The int variable a has a value 10 and its address is referred as an "actual argument" of the function subtract10, when it is called.
subtract10(&a); /* subtract10 function is called */
Function definition - When the subtract10 function is called, it receives the address of the variable(a) passed to it
and this address is copied into pointer variable
named i, which is used to store the address of an int variable, declared in the definition of subtract10 function.
/* subtract10 function is defined */
void subtract10(int *i)
{
*i = *i + 10; /* using * to accessing the value at address */
}
In the function definition, the pointer variable i can also be referred as a "formal argument" of subtract10 function.
This function, uses the * operator which stands for "value at address", which when used with a pointer variable i.e. i, allows us to access the value at an address in the memory location, pointed by i variable.
Eventually, the subtract10 function subtracts 10 from the value contained in i, which is reflected when the value of variable a is printed again on the console after calling the subtract10 function.
Another example of pointers
In the upcoming example, we are going to create a function named swap_int which accepts two int arguments and successfully swaps the value contained in these two char variables
passed to it, because we have called the function and have passed it arguments by reference using pointers. Let's see the code.
// Calling a function with argument, by reference
#include<iostream>
using namespace std;
int main()
{
// function prototype declaration
void swap_int(int *,int *);
int a = 10;
int b = 20;
cout<< "The character value in a is : " << a << "\n";
cout<< "The character value in b is : " << b << "\n";
swap_int(&a,&b); //calling the swap_char function
cout<< "After the swap_char function is called \n";
cout<< "The character value in a is : " << a << "\n";
cout<< "The character value in b is : " << b << "\n";
return 0;
}
/* function to swap characters */
void swap_int(int *c, int *d)
{
int e;
e = *c;
*c = *d;
*d = e;
}
Output-
The character value in a is : 10
The character value in b is : 20
After the exchange function is called
The character value in a is : 20
The character value in b is : 10