The Variable whose value is address of another variable is know as pointer also we should always declare the pointer before using it in our program.
The task are more easily performed using pointer for example, we cannot do dynamic memory allocation without pointers.
Pointers are those variables which we can access through ampersand (&) operator and denotes an address in memory and those variables have their address defined in memory.
#include <iostream>
using namespace std;
int main ()
{
int var1;
char var2[10];
cout << "Address of var1 variable: ";
cout << &var1 << endl;
cout << "Address of var2 variable: ";
cout << &var2 << endl;
return 0;
}
Types of Pointer:
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
You can use pointer in your program like any variable you used.
#include <iostream>
using namespace std;
int main ()
{
int var = 20; // actual variable declaration.
int *ip; // pointer variable
ip = &var; // store address of var in pointer variable
cout << "Value of var variable: ";
cout << var << endl;
// print the address stored in ip pointer variable
cout << "Address stored in ip variable: ";
cout << ip << endl;
// access the value at the address available in pointer
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
0 Comment(s)