This article will tell us all about the basics of using pointers in C++.To begin with a pointer should be understood as a pointer variable like all other variables.Now like a variable of type int can store integer values a pointer variable can store only memory addresses.
First we should understand that when any variable of any data type is declared in the code,a memory is reserved for the variable.If it is a char variable , 1 byte is reserved, for an integer 4 bytes are reserved and so on.We can always get the total memory reserved for the variable by the sizeof() function.
int num=20; cout<<"\n"<<sizeof(num);
Also we can see that the memory address of the variable by & operator
int num=20; cout<<"\n"The size of num is :"<<sizeof(num); cout<<"\n The address of num is"<<#
Declaring a pointer variable
int* my_int_ptr_var ;
This declares a pointer variable of type int.As we understand till now,this pointer variable will store a memory address.Now,the type of the pointer variable tells us that the memory address stored in this pointer variable should be the address of a memory location where an integer is there.
For example,
int num=20; double num2=40.345; int* my_int_ptr_var ; // we use the operator & to get the address of a variable and our pointer variable can store this address ! // now we assign a value to our pointer variable my_int_ptr_var=# my_double_ptr_var=&num2; // and the following will be incorrect,because my_int_ptr_var is of type int and can store only address of integers ! //my_int_ptr_var=&num2; // wrong
Next we are ready to use the pointer variable in our code, the pointer variable is used with the * prefix
*my_int_ptr_var means the value of the integer stored in the memory address contained in my_int_ptr_var
int num=20; double num2=40.345; int* my_int_ptr_var ; double* my_double_ptr_var; // we use the operator & to get the address, and assign them to our pointer variables my_int_ptr_var=# // now we use the pointer variables cout<<"\n The value referenced by my_int_ptr_var is :"<< *my_int_ptr_var; // above will give an out put as "The value referenced by my_int_ptr_var is :20" cout<<"\n The value referenced by my_double_ptr_var is :"<< *my_double_ptr_var; // above will give an out put as "The value referenced by my_double_ptr_var is :40.345"
Now why do we use a pointer variable, as we see above we have two pointer variables of type int and double.In the above code we have assigned the address of num1 and num2 variables to these pointer variables and tried to access the values at those addresses by using the * prefix.
We have the liberty to assign the address of any other integer and double to the pointer variables *my_int_ptr_var and *my_double_ptr_var in our code.The pointer variables once declared can be used to assign the address of any other variable of that type to it.
As you know C++ supports custom data types,in a more advanced manner let us say that there is a structure or a class named employee.
employee* my_employee_ptr_var ; // above creates a pointer variable to store memory address of an employee my_employee_ptr_var = new employee; // above creates a new employee object and assigns the address of newly created object to the pointer variable // we can now reference the members of the employee class using the pointer variable as //get the employee name cout<<"\n Please enter the employee name :"; cin>>*my_employee_ptr_var=>name; //Above assumes that name is a variable in the class
The above was a basic example demonstration of pointers in C++
You can see an advanced use of pointers at
https://techarticleshub.in/double-linked-list-in-c-implementation-tutorial
That is all.
If you liked this article please do leave a reply and share it with friends.
Thanks.