Pointers
The pointer in C++ language is a special type of variable, .It holds/stores the address of
another variable .It is also known as locator or indicator .
Usage of pointer
1. It makes you able to access any memory location in the computer's memory.
2. Pointers save memory space.
3. Dynamic memory allocation
4. Pointers in c language are widely used in arrays, structures, To pass arguments by reference
Seema Kumari ,Astt prof , SCA 1
Declaring a pointer
The pointer in c++ language can be declared using * (asterisk symbol).
Syntax : datatype * variablename ;
Example:
int a; // normal variable (hold value )
int *a; // pointer or say special variable (hold address)
Seema Kumari ,Astt prof , SCA 2
As you can see in the above figure, pointer variable stores the address of
number variable, i.e., fff4. The value of number variable is 50. But the address
of pointer variable p is aaa3.
By the help of * (indirection operator), we can print the value of pointer
variable p.
Seema Kumari ,Astt prof , SCA 3
#include<iostream>
using namespace std;
int main()
{
int b=100;
int *p1=&b;
cout<<"Value of variable b :"<<b<<endl;
cout<<"Address of variable b using pointer : "<<p1<<endl;
cout<<"value of variable b usng pointer "<<*p1;
return 0;
}
Seema Kumari ,Astt prof , SCA 4
dereference operator
We used the pointer variable to get the memory address of a variable (used
together with the & reference operator).
However, we can also use the pointer to get the value of the variable, by using the
* operator (the dereference operator)
Seema Kumari ,Astt prof , SCA 5
Null pointer
A pointer that is not assigned any value but NULL is known as the NULL pointer.
If you don't have any address to be specified in the pointer at the time of
declaration, you can assign NULL value.
int *p=NULL;
Seema Kumari ,Astt prof , SCA 6