Hi, in this blog I will talk about what are pointers, how to create pointers, and related topics like reference variable, dereference variable, and address of operator, this is the topic exclusive to c++ you may not find this in other languages, so let us start π
Pointers
- pointers are one of the most powerful concepts of c/c++ languages as well as confusing and tricky aspects , let's understand it step by step
Address of Operator
Address of operator (denoted by & ) this is an and operator,you may find it in many places like in bitwise and ( for example 89&2 ) you must have seen this & operator but we will talk about its another use case for example
#include<iostream>
using namespace std;
int main(){
int x=5;
cout<<(&x)<<endl;
return 0;
if you run this code you will get something like this 0x61ff0c this is the address of x in hexadecimal, so when you write &x it gives you the address of x.
for now, we are not storing this address but we can store this address in the special variable called pointers.
Pointer variable
- a pointer is a special variable used to store the address of another variable
for example x is an int type variable that has address 342567 and xptr is a special variable that stores the address of variable x .
if you want to create a pointer for a variable x of data type int , you have to just add * after defining data type like int * xptr or for float data type variable you will write
float * xptr
so let's define a pointer
example of Assignment of pointer
int main(){
int x=7;
int * xptr;
xptr=&x;
example of Initialization of pointer
int main(){
int * xptr=&x;
more syntax examples
int *ptr1; //valid syntax
double *ptr2; //valid syntax
int* ptr3; //also a valid syntax
int * ptr4; //also a valid syntax
How to store the address of a pointer?
Storing the address of a pointer is similar to storing the address of a variable as the pointer is also a variable so it has its own address
int main(){
int * xptr=&x;
cout<< & xptr ;
This will give the address of a pointer xptr.
ok, you printed the address of the pointer, what if I want to store the address of a pointer in some variable π₯Έ?
for that you have to create a pointer of a pointer, so we can say xxptr ok π
like this π
example code π
#include<iostream>
using namespace std;
int main(){
int x=4;
//creating pointer for x
int * xptr = &x ;
cout<<"Address of variable x :"<<xptr<<endl;
//pointer to a pointer variable
int **xxptr=&xptr;
cout<<"Address of pointer xptr :"<<xxptr<<endl;
return 0;
}
ok, now you know how to create a pointer and also pointer to a pointer. π
well done you followed along π€π
Next, we will learn about Dereference Operator
Dereference Operator (*)
Dereference Operator is also denoted by *, till now you have seen many use cases of * like for multiplication two numbers or creating a pointer and another use-case we are going to learn that is dereference operator.
refer to this π image
We know that if we do &(variable) we get it address, we previously saw this in the pointer section. but if you do *(address) you will get value store in that address.
In the previous section, we are using * to create a pointer but here we are using it as dereference operator .
If you want to access its value using its address then we will use Dereference Operator
I hope π€ you understand the difference between them.
π‘important point:
- we know if we write &(x) then we will get x's address
- and if we write *(address), we will get the value stored in that address , easy π°
then, tell me what we get if we write *(&x) β π€
obviously π€, we will get the value inside variable x.
example code sample :
#include<iostream>
using namespace std;
int main(){
int x=9;
int * xptr =&x;
cout<<"Address of x :"<<xptr<<endl;
cout<<"Value inside x :"<< *(xptr) <<endl;
cout<<"Value inside x :"<< *(&x) <<endl;
return 0;
}
Output :
Address of x :0x61ff0c
Value inside x :9
Value inside x :9
What is a Null pointer β
sometimes it is useful to make our pointers point to nothing. This is called the Null pointer.
We can assign a pointer a null value by setting it to address 0 for example:
int *p=0;
//or
int * q = NULL;
π‘Important point :
- you cannot dereference a null point.
Reference Variable
A reference variable is a very powerful concept in c++ and it will allow us to pass objects by reference
We have used & operator in many places such as bitwise (example 5&4), to get the address of variable by &(variable) and now we will use the operator & in the Reference variable.
see this π reference image
- In the first case, we assigned the variable y the value of variable x.
- but in the second case we are not creating a new variable, we are just assigning two names (as you know variables are name labels) so we are giving two name labels for one address that contains some value.
So basically Reference variables create another name or create an alias for the same object.
example code:
#include<iostream>
using namespace std;
int main(){
int x=10;
int &y = x;
cout<<"address of x :"<<&x<<endl<<"address of y :"<<&y<<endl<<"value of x :"<<x<<endl<<"value of y :"<<y<<endl;
return 0;
}
output:
address of x :0x61fef8
address of y :0x61fef8
value of x :10
value of y :10
Another example(either you increase x or y both of them will change ):
#include<iostream>
using namespace std;
int main(){
int x=10;
int &y = x;
y++;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
return 0;
}
output :
x=11
y=11
Pass by Reference using the Reference variable
To understand pass-by reference using reference variable first I will tell you what is the problem with pass by value.
see this π code :
#include<iostream>
using namespace std;
//pass by value
void add_2(int n){
n= n+2;
}
int main(){
//pass by value
int n;
cin>>n;
add_2(n);
cout<<n;
return 0;
}
output :
5
5
Two copies of n are created and changes is not reflected so instead of having two copies we must have only one copy so changes can be reflected , for this we should pass value to the reference variable
example:
#include<iostream>
using namespace std;
//pass by reference
void add_2(int &n){
n= n+2;
}
int main(){
//pass by value
int n;
cin>>n;
add_2(n);
cout<<n;
return 0;
}
output :
5
7
Thank you ππ for reading, I hope you understand everything if you have any doubt let me know π in the comment π