How to Declare Function Pointer in C?

How to Declare Function Pointer in C.

Pointers can be used with function in several ways:

  • Passing Pointer as parameter to function.
  • Returning a pointer.
  • Pointer to a function.

Let us see How to Declare Function Pointer in C one by one.

1. Passing Pointer to Function

Parameters can be passed to a function in two ways: call by value and call by reference.

In call by value, the value of a variable is passed to a function. In ‘C’, all parameters are passed by value, i.e., the value of the actual argument gets copied into the formal or dummy argument.

However, any changes made in the function will be made only to the formal argument and not to the original variable.

In call by reference, the address of a variable is passed to a function. In order to modify the original variable, we should pass its address to the function.

This address is stored in a pointer in the function as an argument. This pointer allows the original variable from the function. How to declare function pointer in c.

Syntax: return_type function(datatype * pointer);

Example:

void display(int *ptr);

int string_length(char *str);

void swap(int *p1, int *p2);

The following program shows how to swap the values of two variables using a function.

//Learnprogramo
#include<stdio.h>
void main()
{
void swap(int *,int*);
int n1=10, n2=20;
printf("Enter the two numbers: ");
scanf("%d%d",&n1,&n2);
swap(&n1, &n2);
printf("\n The swapped values are %d %d",n1,n2);
}
void swap(int *ptr1, int *ptr2)
{
int temp;
temp=*ptr1; *ptr1=*ptr2; *ptr2=temp;
}

Output:

how to declare function pointer in c

Passing Array to Function Using Pointer

An array name is a pointer to the array. When we pass an array to a function, we are actually passing the base address of the array. This address can be stored in a pointer.

The following program illustrates how array elements are accessed using a pointer.

//Learnprogramo
#include<stdio.h>
void main()
{
int a[5]={1,2,3,4,5};
void display(int *ptr, int n);
display(a,5);
}
void display(int *ptr, int n)
{
int i;
printf("Array elements are:\n ");
for(i=0; i<n;i++)
printf("%d",ptr[i]);
}

Output:

how to declare function pointer in c

2. Function Returning a Pointer

A function can return a pointer. The function header is declared as:

Syntax:

datatype * function(argument list);

Example:

int *f1(int); f1 is a function that accepts an integer parameter and returns a pointer to an integer.

char *f2(int*, int*); f2 is a function which accepts two integer pointers and returns a pointer to char.

In the following program, a function accepts the address of two variables and returns the address of the variable having the larger value.

//Learnprogramo
#include<stdio.h>
void main()
{
int *larger(int*, int*);
int n1,n2, *maxptr;
printf("Enter the two numbers:");
scanf("%d%d",&n1,&n2);
maxptr=larger(&n1, &n2);
printf("\n The larger value is %d", *maxptr);
}
int *larger(int *ptr1, int *ptr2)
{
if(*ptr1>*ptr2)
return(ptr1);
else
return(ptr2);
}

Output:

how to declare function pointer in c

3. Pointer to Function

A pointer points to a memory location where a variable is stored. It can also point to the beginning of executable code such as a function in memory.

Declaration:

A pointer to a function is declared using the syntax:

return_type (*function_name) (argument);

Example:

int (*ptr_f) (int, int);

Here, ptr_f is a pointer to a function which takes two integer arguments and return an integer.

Initialization:

To use this pointer, it must be assigned the address of a function. Let us consider a function max which returns the maximum of two numbers.

int max(int x, int y)
{
return x>y?x:y;
}

The pointer is initialized as follows:

ptr_f = max;

Function Call:

The function can be called using the pointer as follows:

int result = ptr_f(10,20);

The following program shows how to declare function pointer in c.

//Learnprogramo
#include<stdio.h>
int main()
{
int max(int,int);
int (*ptr_f) (int,int);
int a,b,result;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
printf("Call using function name\n");
result=max(a,b);
printf("Maximum= %d", result);
printf("\nCall using function pointer\n");
ptr_f=max;
result=ptr_f(a,b);
printf("Maximum=%d", result);
return 0;
}
int max(int x, int y)
{
return x>y?x:y;
}

Output:

how to declare function pointer in c

Conclusion:

In this lesson, we have learned about How to declare function pointer in C. Now, in the next lesson we will learn dynamic memory allocation in C.

Also Read: