Pointer to Pointers, Function, Array in C

November 11, 2013 | By in HOWTOS, PROGRAMMING
| Reply More

c pointer

In one of our earlier articles, we discussed about fundamentals of pointers in C. In this article, we will discuss some of the advanced C pointer related concepts like pointer to pointers, function pointers and pointer to arrays. All the explanation will be accompanied by practical examples.

Testing Environment

Before jumping on to the concepts, here are some details of the testing environment :

  • OS - Ubuntu 13.04
  • Compiler - GCC 4.7.3
  • cdecl 2.5

Pointer To Pointer

In the first part, we learned that pointers are special variables that hold the addresses of other variables. Or, in a layman's term, point to other variables.

For example :

int a;
int *ptr = &a;

In the example above, we can say that the pointer ptr points to an integer ('a' in this case).


char a;
char *ptr = &a;

Here we can say that the pointer ptr points to a character variable.

But, can we do something like :

char a;
char *ptr1 = &a;
char *ptr2 = &ptr1

and say that pointer ptr2 points to ptr1? No, because the pointer ptr2 is of character type which means that it can point to character variables only but ptr1 is not a character variable. So the third line above is incorrect.

Following is the correct way of declaring a pointer to pointer :

char **ptr2 = &ptr1

The pointer ptr2 now contains the address of pointer variable ptr1. Now, to access the character 'a' through the pointer ptr2, always use **ptr2.

Here is an explanation of how **ptr would give you the value of variable 'a' :

  • ptr2 contains the address of ptr1
  • *ptr2 would give us the value kept at the address of ptr1. This value is nothing but the address of variable 'a'.
  • **ptr2 would give us the value kept at address of 'a' which is nothing but value of variable 'a'.

NOTE - You can have pointer to pointer, pointer to pointer to pointer and so on up to infinite levels (though the upper limit is implementation specific) but more than 2-3 levels are not used in real life projects written in C programming language.

Here is an example of pointers to pointers in C:


void func(char **p)
printf("\n Pointer p points to [%s]\n", *p);

int main(void)
char *ptr = "Linoxide";

return 0;e

Following is the output of this program :

$ ./pointer2pointer

Pointer p points to [Linoxide]

Function Pointers

Just like there are pointers to variables and pointers, there can be pointers to functions also. Here is how a pointer to a function can be declared:

[return-type](*func-pointer-name)(arg1-type, arg2-type, ...)

In the syntax above :

  • return-type is the actual return type of the function being pointed by the pointer
  • func-pointer-name is the name of the pointer as chosen by the programmer
  • arg-type is the type of arguments that the function accepts

Here is an example of a function pointer :

(int)(*fptr)(int, int)

Going by the declaration, this function pointer points to a function that accepts two integer arguments and returns an integer value. Once declared, a function pointer can be made to point a function in following way :

fptr = func;


fptr = &func;

And finally, the pointed function can be called through a function pointer in the following way :

fptr(arg1, arg2, ...);

Here is a working example of function pointer :


int sum(int a, int b)
printf("\n Inside sum() \n");
return (a + b);

int main(void)
int ret = 0;

fptr = sum;

ret = fptr(5, 10);

printf("\n The function sum() returned [%d]\n", ret);

return 0;

Following is the output of this program :

$ ./func_ptr

Inside sum()

The function sum() returned [15]

In practical sense, function pointers are mostly used while implementing call back functions.

Pointer to arrays

Just like there are pointers to functions, there can also be pointers to arrays. The declaration of a pointer to array is somewhat similar to a function pointer.

Here is the syntax :


So, in the above declaration:

  • array-type represents the type of elements that the array contains
  • As the name suggests, array-pointer-name is the name of array pointer
  • size represents the size of array.

Here is an example declaration :


The above line declares an array pointer 'arr' that points can point to an array of 5 integers.

Please do not confuse pointer to an array with following declarations :

int* arr[5];

int *(arr[5]);

As both of these lines declare an array of 5 integer pointers.

If you ever have any confusion related to pointers and arrays, use the cdecl utility. It is an awesome utility that will provide you answers to your questions/confusions just like one of your intelligent friend.

Here is an example :

$ cdecl
Type `help' or `?' for help
cdecl> explain int(*arr)[5]
declare arr as pointer to array 5 of int
cdecl> explain int* arr[5];
declare arr as array 5 of pointer to int
cdecl> explain int *(arr[5]);
declare arr as array 5 of pointer to int

Not only this, you can also do the reverse. This means that you can provide cdecl with explanation and it will give you back the expression.

Here is an example :

cdecl> declare arr3 as array 5 of pointer to int
int *arr3[5]

So you can see that how easily you can ask cdecl to explain an expression and vice-versa.


Share This :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.