Вы находитесь на странице: 1из 10


In simple words, a pointer is an address. It is a derived data

type that stores the memory address. Pointers in C++
language is a variable that stores/points the address of another
variable. A Pointer in C++ is used to allocate memory
dynamically i.e. at run time. The pointer variable might be
belonging to any of the data type such as int, float, char,
double, short etc.
How does Pointer Work?

& Reference or address

* Value at
Declaring a pointer

int *ip; /* pointer to an integer */

double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
Key points to remember about pointers

•Normal variable stores the value whereas pointer variable

stores the address of the variable.
•The content of the C++ pointer always be a whole number
i.e. address.
•Always C pointer is initialized to null, i.e. int *p = null.
•The value of null pointer is 0.
Assigning addresses to Pointers
Pointer Arithmetic in C++

Pointer arithmetic There are four arithmetic

operators that can be used in pointers:
++, --, +, -

The Rule to increment the pointer is given


new_address= current_address + i * size_of(data type)

Where i is the number by which the pointer get increased.

C++ Pointer Addition

We can add a value to the pointer variable. The formula of adding value to
pointer is given below:
new_address= current_address + (number * size_of(data type))

int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
cout<<"Address of p variable =“<<p;
p=p+3; //adding 3 to pointer variable
cout<<"After adding 3: Address of p variable is”<<p;

As you can see, the address of p is 300. But after adding 3 with p variable, it
is 306, i.e., 2*3=6 increment.
Pointer Expression How it is evaluated ?
ip => ip + 1 => 1000 + 1*2 =>
ip = ip + 1
ip++ => ip + 1 => 1002 + 1*2
ip++ or ++ip
=> 1004
ip => ip + 5 => 1004 + 5*4 =>
ip = ip + 5
ip => ip - 2 => 1024 – 2*2 =>
ip = ip - 2
ip => ip + 2 => 1020 + 2*2 =>
ip-- or --ip
Combining Indirection operator (*) and Increment/Decrement

Suppose x is integer variable and p is a pointer to int. Now consider the

following statements and try to interpret them.

Example 1:

x = *p++;

Since * and ++ operators have the same precedence and associate from right
to left ++ will be applied to p, not to *p. Because the increment operator is
postfix, so first the value of p is used in the expression then it will be
incremented. Therefore, the first integer pointed by p will be dereference and
assigned to x, then the value of p will be incremented by 1
Example 2:

x = ++*p;

Here * operator will be first applied to p then ++ will be applied to *p. Therefore
first integer pointer is dereferenced, the value obtained from dereferencing is
incremented and eventually assigned to x.

Example 3:

x = *++p;

++ operator is prefixed, so first, p will be incremented, then the value at the

new address is dereferenced and assigned to x.

Note: If you still have any confusion, you can always use () around
expression which you want to evaluate first.