Pointers in c++

C++ Pointers ?

A pointer is a variable that holds the memory address, usually the location of another variable in the memory.


The pointer are one of the C++'s most useful and strongest features.The correct understanding and use of pointers is critical to successful programming in C++.

Pointers are symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. It’s general declaration in C/C++ has the format:

Why Pointers are so Important ?

Pointers provides the means by which the memory location of a variable can be directly accessed and hence can be manipulated in the way as required.

Pointers supports C++'s dynamic allocation routines.

Pointers can improve the efficiency of certain routines


C++ Pointer Initialization :

int num = 20;// declares an variable named num
int *iptr;// declares an int pointer named iptr
iptr = #// this stores the memory address of num into iptr

Let's look at below statements to understand the concept:

int num = 20;
int *iptr;
iptr = #
int val = *iptr;

From the above statements, first a variable num of type int is created and initialized with value of 20.
Now a pointer variable iptr of type int is declared. And the address of the variable num is initialized to the pointer iptr.
Now the value present at the address num is initialized to the variable val of type int which is 20 using the operator*.

Example :



Output :

 Statements:
 	int num =20;
 	int val;
 	int *ptr;
 	iptr = #
 	val = *iptr
Output:
	num = 20
	val = 20