Pointer is a address variable that is meant or storing address of data not data itself.

-> pointer are used for indirect accessing the data.

Q) What is need of the pointer to accessing the data indirectly.
->(i) program not directly access heap memory by the use of pointer we can access heap memory.

(ii) By the use of pointer we can access any file type.

(iii) All these external things, resources will be access by using pointer.

* Use of Pointer
(i) Accessing heap
(ii) Accessing Resources
(iii) Parameter Passing

declaration and initialization of pointer.
-> int a=10 // declaration of data variable
int *p; //declaration of pointer
p=&a; // initialization of pointer

printf(“%d”, a); ————- 10
printf(“%d” , *p); ————– 10
printf(“%d”, p); —————– 200 (address of main -memory)

Accessing heap memory by using pointer
-> By using malloc Functio.
int main()
int *p;
p=(int *) malloc (5* sizeOf(int));
//here, (int* )is return type of malloc function by default malloc function return void and (5* sizeOf(int)) is the size allocated in heap memory.

Note: In c language we access heap by using malloc() but in c++ by using new keyword.
skeleton for accessing heap in c++
p=new int [5]; //here, we are accessing array.


Related post

Leave a Reply

Your email address will not be published. Required fields are marked *