Operations on pointers - C

  • Increment-decrement.
  • Adding a number to pointer.
  • Subtracting a number from a pointer.
  • Subtraction of one pointer from another.
  • Comparison of two pointer variables.
  • Number of operation can be performed on pointers since they are variables.

Operations like incrementing pointers, decrementing pointers, subtracting pointers from each other, comparing them and subtracting a number from pointer are possible. But some operations seem absurd and would give unpredictable results if performed.

Increment – decrement

int * pt;

pt ++ increment pt to point to next location of same type. What do we mean by same type? If pt is a pointer to integer, integers occupy 2 bytes. So if pt = 4001, pt++ gives pt = 4002.
pt-- decrements pt to point to previous element of the same type.
This will be clear if we consider pointers with reference to arrays whose elements are placed in consecutive memory location.

Adding a number to a pointer A number can be added to pointer


/* j points to 9 integer locations after current location */

Subtracting a number from a pointer
A number can be subtracted from a pointer


/* j point to 6 integer location after current location */

Subtraction of one pointer from another
One pointer variable can be subtracted from another, provided both point to elements of same array. The result is the number of elements between corresponding array elements.

(j – i) gives the difference between address
(*j - *i) gives the difference between values contained in address at j and i

Comparison of two pointer variables
Comparison of two pointer variables is possible provided both point to one array (i.e. both pointers point to objects of same data type). Pointers can also be compared with zero(NULL).

The two pointers point to same location.
The operation of adding two pointers, multiplication of a pointer with a constant, division of a pointer with a constant should not be performed.

Pointer to pointer
A pointer to a variable is a variable that stores address of another variable of a specific type. A pointer to pointer is a variable that stores address of a pointer variable of specific type.

/* Program demonstrates pointer to pointer */

In the above example
i is an ordinary integer variable
j is a pointer variable which contains address of i
k is a pointer variable which contains address of j. j is a pointer hence k is a pointer to pointer.

The following figure can make things clear

Pointer to pointer
The address given here are for explanation purpose. You may get different addresses on your computer. Thus address of i can be obtained also in terms of j and k as given in example program. Also value of i can be obtained in terms of j and k. value of i as *(&i) is same as *j because &i = j.

Array of pointers
We have seen array of integers, array of characters. Similarly we can also have array of pointers. Since pointers are nothing but variables which store addresses. Array of pointers is a collection of addresses.

The format is

Data-type * array-name [expression]

The square bracket of array takes precedence over the ‘*’ operator. It is array of pointer to specific data-type. The address can be address to individual variable or address of array elements. The following program illustrates the basic concept of array of pointers.

//Array of pointers

int *a[4] declares an array a as an array of pointers (which is indicated by the *). It is pointers to integers. i.e. ‘a’ is a collection of addresses which contains addresses of integers.

The next line defines integers i, j, k, l. The addresses of these integers are assigned to each array elements. We know &i is address of i and so on.

In for loop, m run from 0 to 3. a[m] gives an address of integer. ‘*’ gives contents of address i.e. *a[m] gives contents of address a[m].

The difference between ordinary variable and pointer variable is that ordinary variable stores value of specific type, while pointer stores address of specific type. Same difference exists between array and array of pointers. Array is a collection of values. Array of pointers is a collection of address.

Array of pointers is a collection of address
We have said, address can also be addresses to array or array elements. This is a very interesting and important feature. We have studied multidimensional arrays. The name of multidimensional array is a pointer to group of arrays. A multidimensional array can be expressed in terms of array of pointers rather than as a pointer to group of continuous arrays.

We can use

data-type * array-name [expression 1]

instead of

data-type * array-name[expression 1 ][expression 2]

The last expression is omitted when defining array of pointers With the following example, let us see relation between multidimensional array and array of pointers. We have a two-dimensional integer array with 10 rows and 20 columns.

relation between multidimensional array and array of pointers.
If we want to access x[2][5] it can be done by
*(x[2] + 5)
x[2] gives address of third row.
(x[2] + 5) gives address of fifth element in third row.
*(x[2] + 5) gives contents

Array of pointers to strings are widely used. The n-element array can point to n different strings. The two dimensional array of characters ‘nam’ can be stored in array of pointers.

char * nam [] = { “Akshay”,

The need to use pointer instead of multidimensional arrays can be explained at this place. In twodimensional arrays ‘nam’, a memory space of 5 * 10 = 50 bytes is allocated straight away. While in pointer array, (number of character in all rows added) = 29 bytes and 10 bytes for address i.e. 29 + 10 = 39 bytes are used. Thus there is a lot of saving in space. Secondly, string can be manipulated with ease using pointer arrays.

/* Rewriting example (2) in strings chapter, Exchanging strings using pointers instead of arrays

Original Names : raman srinivas
New Names : srinivas raman
A temporary pointer to character is defined and only the addresses of names are exchanged instead of the names themselves.

Pointer to function
The idea may seem strange but it exists. As we can have address of integer variable, character variable, array, we can also get address of a function. Thus we have a pointer to function. So we can invoke functions using their pointers instead of the usual way.

Address of function display is 419872
Example of pointer to function

Above is a simple program, which gives a normal call to function ‘display’. Now if we want to invoke function, using pointer we will need the address of the function. How do we get the address of function? The name of the function by itself without the round brackets after it, gives the address of function.
The program given below invokes function display using its address

/* program demonstrating pointer to function it invokes function
display using its address */

Let us study the program statement by statement. The statement in main, int display(), declares that function returning integer is used in main. func_ptr is a pointer to function which return an integer. If you have understood how to define pointer to variable and array of pointer, you will understand this definition quickly.
Pointer to integer int *p;
Array of pointers to integer int *p[];
Pointer to function returning integer int(*p)();
Function returning pointer to integer int *p();

We use round brackets around the ‘*’ operator because we want the pointer to precede the function brackets. If we say int *p(), it defines a function returning a pointer to integer. Here function brackets take precedence. Thus func_ptr is a pointer to a function returning an integer. The address of function display is assigned to func_ptr in the third statement. First pointer to function is defined then an address is assigned to it. The basic remains the same no matter pointer points to any object.(*func_ptr)() invokes the function display since func_ptr now points to display.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics