Pointers as Function Argument in C
Pointer as a function parameter is used to hold addresses of arguments passed during function call. This is also known as call by reference. When a function is called by reference any change made to the reference variable will effect the original variable.
Example Time: Swapping two numbers using Pointer
void swap(int *a, int *b);
int m = 10, n = 20;
printf("m = %d\n", m);
printf("n = %d\n\n", n);
swap(&m, &n); //passing address of m and n to the swap function
printf("m = %d\n", m);
printf("n = %d", n);
pointer 'a' and 'b' holds and
points to the address of 'm' and 'n'
void swap(int *a, int *b)
temp = *a;
*a = *b;
*b = temp;
m = 10
n = 20
m = 20
n = 10
Functions returning Pointer variables
A function can also
return a pointer to the calling function. In this case you must be careful, because local variables of function doesn't live outside the function. They have scope only inside the function. Hence if you return a pointer connected to a local variable, that pointer will be pointing to nothing when the function ends.
int* larger(int*, int*);
int a = 15;
int b = 92;
p = larger(&a, &b);
printf("%d is larger",*p);
int* larger(int *x, int *y)
if(*x > *y)
92 is larger
Safe ways to return a valid Pointer.
- Either use argument with functions. Because argument passed to the functions are declared inside the calling function, hence they will live outside the function as well.
- Or, use
static local variables inside the function and return them. As static variables have a lifetime until the
main() function exits, therefore they will be available througout the program.
Pointer to functions
It is possible to declare a pointer pointing to a function which can then be used as an argument in another function. A pointer to a function is declared as follows,
Here is an example :
int (*sum)(); //legal declaration of pointer to function
int *sum(); //This is not a declaration of pointer to function
A function pointer can point to a specific function when it is assigned the name of that function.
int sum(int, int);
int (*s)(int, int);
s = sum;
s is a pointer to a function
sum can be called using function pointer
s along with providing the required argument values.
s (10, 20);
Example of Pointer to Function
int sum(int x, int y)
int main( )
int (*fp)(int, int);
fp = sum;
int s = fp(10, 15);
printf("Sum is %d", s);
Complicated Function Pointer example
You will find a lot of complex function pointer examples around, lets see one such example and try to understand it.
void *(*foo) (int*);
It appears complex but it is very simple. In this case
(*foo) is a pointer to the function, whose argument is of
int* type and return type is