Pointers in C

A pointer is a variable suitable for keeping memory addresses of other variables. The values you assign to a pointer are memory addresses of other variables or other pointers.

Pointers in C are characterized by their value and data-type. The value is the address of the memory location the pointer points to, the type determines how the pointer will be incremented/decremented in pointer or subscript arithmetic.

Pointers are used to manipulate arrays and they can be used to return more than one value from a function.

Pointers are declared by using the asterisk(*).
int *p;

Each variable has two attributes: address and value. The address is the location in memory. In that location, the value is stored. During the lifetime of the variable, the address is not changed but the value may change.

void main (void)
{

int i;
int * a;
i = 10;
a = &i;
printf (” The address of i is %8u \n”, a);
printf (” The value of i is %d\n”, i);
printf (” The value at that location is %d\n”, *a);

}

Output

The address of i is 631672
The value of i is 10
The value at that location is 10

Arrays and Pointers

An array is actually a pointer to the 0th element of the array. Dereferencing the array name will give the 0th element. This gives us a range of equivalent notations for array access. In the following examples, arr is an array.

Array
Pointer
arr[0] *arr
arr[1] *(arr+1)
arr[n] *(arr+n)

Leave a Reply

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