How to use pointers in C++ ?

Pointers:-
       Now we will learn about pointer in c language. Pointer is a type of variable which stores the address of another variable as its value.
To know more about pointers here is the link :
https://newageinformers.blogspot.in/2016/10/pointers-of-c-program.html

In this post we will learn how can we use pointers in c++;

Declaration of pointer:-
        data type  *pointer-name;
           
int *x;  (* = pointer)
int y;
x=&y;  (& = address of y)

In above example, (*x) is represent pointer variable and x= &y means that x variable is storing the address of y variable.
Let us know by program,

#include<iostream.h>
#include<conio.h>
void main()
{
int x;
clrscr();
x=6;
cout<<"\n value of x is =  "<<x;
cout<<"\n address of x is = "<<&x;
getch();
 }
In the output, value of x will be 6 and address of x will be 0x8f8cfff4. It is the memory location of x.

Another example is how variable can be accessed by pointer.

#include<iostream.h>
#include<conio.h>
void main()
{
int a,x, *y;
clrscr();
cout<<"\n Enter radius of the circle";
cin>> a;
x= 3.14*a*a;
y= &x;
cout<<"\n value of x = "<< x;
cout<<"\n address of x= "<<&x;
cout<<"\n address of y = "<<y;
cout<<"\n value stored at y = "<<*y;
getch();
}
 In above program, we are not returning value from main function so we wrote void.
In main function, we assumed three variables. y is a pointer variable and it will store the address of x variable as a value.

So this is about how we use pointer. My next post will be based on accessing pointer through array and string.

Until then Bye Bye!!!

Follow us here for different updates:

facebook: CLICK HERE!!!!!!!!!!
 
Twitter: @NewAgeInformers
 
Instagram: @new_age_informers_

Comments