Pointers in C
Pointers in C
Definition
A Pointer is a variable that holds address
of another variable of same data type.
also known as locator or indicator that
points to an address of a value.
Benefits of using pointers
Pointer reduces the code and improves the
performance, it is used to retrieving
strings, trees etc. and used with arrays,
structures and functions.
We can return multiple values from
function using pointer.
It makes you able to access any memory
location in the computer's memory.
It allows C to support dynamic memory
management.
Concept of pointer
Whenever a variable is declared in the program,
system allocates a location i.e a name to that
variable in the memory, to hold the assigned
value. This location has its own address number.
Let us assume that system has allocated memory
location 80F for a variable a.
int a = 10 ;
We can access the value 10 by either using the
variable name a or the address 80F. Since the
memory addresses are simply numbers they can be
assigned to some other variable. The variable that
holds memory address are called pointer variables.
A pointer variable is therefore nothing but a
variable that contains an address, which is a location
of another variable. Value of pointer variable will
be stored in another memory location.
Declaring a pointer variable
In the above case, pointer will be of 2 bytes. And when we increment it, it
will increment by 2 bytes because int is also of 2 bytes.
In this case, size of pointer is still 2 bytes. But now, when we increment it,
it will increment by 4 bytes because float is of 4 bytes.
Similarly, in this case, size of pointer is still 2 bytes. But now, when we
increment it, it will increment by 8 bytes because its data type is double.
32 bit Machine (Visual Basic C++)
is same as:
Array of pointers
Example, which uses an array of 3 integers −