c++ Pointers

C++ Pointers

In this article, we will learn about the concept of pointers in CPP and how to use pointers in our programming in CPP.

What is Pointers?


First of all, understand the memory address of the variable. Variable takes space from the memory of the computer. Computer memory allocates space to the variable with a memory address. 


This memory address is used to find the variable in the memory. Before the pointer, we use the variable name for accessing the variable value. 

But we can also use the memory address of the variable for accessing the value of the variable. In the pointers, the memory address of the variable is used to access the variables.


How to access the address of the variable?

Address in c++:


In the CPP language, ampersand sign (&) is used to find the memory address. The ampersand sign is known as the address of the operator. If we have the num variable, address of variable access(&num) and the value of variable access (var).For example:


#include <iostream>
using namespace std;

int main() 
{
int num1 =2,num2 =3,num3 =5;

//display the value and address of the variables

cout<< num1 <<" address ="<<(&num1)<<endl;        //display the first number
cout<< num2 <<" address ="<<(&num2)<<endl;        //display the second number
cout<< num3 <<" address ="<<(&num3)<<endl;        //display the 3rd number

return 0;

}



Output

2 address =0x6ffe1c
3 address =0x6ffe18
5 address =0x6ffe14


The address of any variable decides during the run time. The memory address is not known before the run time of the program.

In order to clarify, understand the following code fragment.

var=20;
var1=&var;
var2=var;

After the execution the value contained in the variables.

pointer


In the first statement, assign the value 20 to the var
In the second statement, assign the address of the var to the var1,By using &address of var assigned.
In the third statement, assign the var to the var2.


look at the second statement,var1 contains the address the var.
Var1 is the variable that contains the address of the variable is called the pointer of var.
Pointer is the variable that stores the address of other variables.

By pointer, we can access the address of the variable as well as the value of the variable.

How to dereference the pointers?


Dereference of pointers


The deference of the pointer used for accessing the value of the variable.For deference the use (*)before the pointer then pointer gives the value of the variable which address store in the pointer.

num=*var1;

Example:

#include <iostream>
using namespace std;

int main() {

int var=20;

int *ptr;//declare pointer

int num1,num2;

ptr=&var;

//deference the pointer and assign the value to num1

num1=*ptr;
cout<<num1<<endl;

return 0;

}


Output
20

pointer




Actually, the address of operator and dereference of the pointer are a compliment of both each other
&var is the address of operator
*var is the  dereference
then value of variable access

ptr=&var
*ptr=*(&var) = var
 
In the above two statements, store the address of the variable in the ptr and in the second statement dereference the pointer. (*)(&) both cancel the effect of each other.

How to declare the pointers in C++?

Declaration of Pointers:


Pointer has a unique ability to accessing the variable in the memory. Also, pointers are different for all data types. For int data type, there will be a pointer of int present, as same for double, char, and float.

The size of the pointer is the same for all data types because space occupies by pointer depend on the architecture of the environment.

The declaration of the pointer

datatype *pointer name

Here, the data type of pointer writes before the pointer and uses (*)for declaring the pointer. This datatype tells about the data for which pointer would be used for pointing.
For example

int *ptr1;
float *ptt;
char *ch;

There is the three-pointer int, float, and char. All these occupy the same space in the memory but the data which they pointed is not occupied the same space in the memory. The size of the pointer depends on the environment in which the program runs.

Note that in the declaration of the pointer (*)asterisk is used. This is the syntax to declare a pointer.It should not be confused with the deference operator(*) earlier in the above but they are totally two different things.

For example


#include <iostream>
using namespace std;

int main() {

int number=20;
float number2=40.5;
char number5='a';


int *ptr=&number;//declare int pointer

float *pp=&number2;//declare float pointer

char *ch=&number5;//declare character pointer


//deference the pointer and print the value of the variables


cout<<(*ptr)<<endl;
cout<<(*pp)<<endl;
cout<<(*ch)<<endl;

return 0;

}


Output

20
40.5
a

In this example, three-pointers declare of int, float, and char types and three variables of int, float, and char types. Pointed the pointer of int to int variable, the pointer of float to float variable, and the pointer of char to char variable. Because the data type of pointer and variable it is pointed both are same.


At the end of the program, display the value of a variable with the help of the pointers. Simply, deference the pointer and access the value of the variable.

Here another example for more explanation
Example

#include <iostream>
using namespace std;

int main() {

int number1,number2;
number1=10;
number2=20;
int *ptr1,*ptr2;//decalre two pointer of int type

ptr1=&number1;
ptr2=&number2;

*ptr1=*ptr2;//assign the ist pointer to the 2nd pointer


*ptr2=30;// change the value of the 2nd variable


cout<<" 1st number "<<number1<<endl<<" 2nd number "<<(number2);


return 0;

}

Output

Ist number 20
2nd number 30

In the above example, declare two int variables and two-pointer of int type. Assign the value of the 2nd pointer to the 1st pointer by(*ptr1=*ptr2).In the statement (*ptr2=30) change the value of the 2nd variable by helping pointer. By display 2nd number, the value is changed to 30.


Pointers with arrays


In this concept, first of all, understand the concept of the array
The array is the type of the pointer but the difference is that array is the constant pointer.The array does not change the original position, only displace by the help of indexes.

Consider the following two declarations 
int *ptr;
int array[50];


Look at the two operations
ptr=array;
array=ptr;

The first operation is valid but the second operation is not valid. After the first operation, ptr and array would be acting in the same way. Both used for the elements of the array. In the second operation, array only represents the 50 elements of int type, array never is assign anything. Therefore second operation invalid.


Example:

#include <iostream>
using namespace std;

int main() {

int *ptr;
int array[5];

ptr=array;
array[0]=10;//first index

ptr[1]=20;//second index

array[2]=30;//third index

ptr[3]=40;//fourth index

array[4]=50;//fifth index

//use for loop for displaying the element
for(int i=0;i<5;i++)
{
cout<<array[i]<<" ";
}



return 0;

}

Output:

10 20 30 40 50


In the above example, one pointer and one array of int type, equate the pointer and array because both are pointers. The pointer ptr also pointed to the same address as an array pointed. Now, we can use ptr as use as an array. In the remaining part of the code, the ptr pointer is used for accessing the element of the array. In the end, use to display the values store in the array.


Pointer to Pointer

Likewise,In the array increase the dimensions of array,C++ allows to point the pointer to the pointer.
only use more asterisk(*) in the syntax for increase the in the dimension of the pointer.

Example:

#include <iostream>
using namespace std;

int main() {

int num;
int *p;
int **pp;

num=10;
p=&num;
pp=&p;

cout<<(**pp)<<endl;
cout<<(*p)<<endl;
cout<<(num)<<endl;

return 0;

}

Output
10
10
10

In the above example, access the value of the variable 1D pointer and 2D pointer.1D pointer one time dereference to reach the value of the variable but 2D pointer two times dereference to access the value of the variable.

Please write the comment,if you find anything incorrect or you want to share more information about the above topic.

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

How to format in HTML