Pointers

Pointer are a fundamental part of C. If you cannot use pointers properly then you have basically lost all the power and flexibility that C allows. The secret to C is in its use of pointers.

What is a Pointer?

A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.

The unary or monadic operator & gives the “address of a variable”.

The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.

To declare a pointer to a variable do:

int *pointer;

NOTE: We must associate a pointer to a particular type: You can’t assign the address of a short int to a long int, for instance.

Consider the effect of the following code:

int x = 1, y = 2;

int *ip;

ip = &x;

y = *ip;

x = ip;

*ip = 3;

It is worth considering what is going on at the machine level in memory to fully understand how pointer work.

Advantages of using pointers are
1.) Function cannot return more than one value. But when the same function can modify many pointer variables and function as if it is returning more than one variable.
2.) In the case of arrays, we can decide the size of th array at runtime by allocating the necessary space.

Disadvantages of pointers
1.) If sufficient memory is not available during runtime for the storage of pointers, the program may crash (least possible)