The Wayback Machine - https://web.archive.org/web/20240711064110/https://www.geeksforgeeks.org/how-to-declare-a-pointer-to-a-function/
Open In App

How to declare a pointer to a function?

Last Updated : 11 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

While a pointer to a variable or an object is used to access them indirectly, a pointer to a function is used to invoke a function indirectly.

Well, we assume that you know what it means by a pointer in C. So how do we create a pointer to an integer in C?

Huh..it is pretty simple…

int *ptrInteger; /*We have put a * operator between int 
                    and ptrInteger to create a pointer.*/

Here ptrInteger is a pointer to an integer. If you understand this, then logically we should not have any problem in declaring a pointer to a function 🙂

So let us first see ..how do we declare a function?

int foo(int);

Here foo is a function that returns int and takes one argument of int type. So as a logical guy will think, putting a * operator between int and foo(int) should create a pointer to a function i.e.

int *foo(int);

But Oops..C operator precedence also plays a role here ..so in this case, operator () will take priority over operator *. And the above declaration will mean – a function foo with one argument of int type and return value of int * i.e. integer pointer. So it did something that we didn’t want to do. 🙁

So as a next logical step, we have to bind operator * with foo somehow. And for this, we would change the default precedence of C operators using () operator.

Example

int (*foo)(int);

That’s it. Here * operator is with foo which is a function name. And it did the same as what we wanted to do. So that wasn’t as difficult as we thought earlier!

Let’s see an example in C to understand how to declare a pointer to a function.

C




// C Program to illustrates how to declare a function
// pointer
#include <stdio.h>
int add(int a, int b) { return a + b; }
 
int main()
{
    // Assigning function address using & operator
    int (*add_ptr)(int, int) = &add;
    // or
    // Assigning
    // function address without & operator
 
    // int (*add_ptr)(int, int) = add;
 
    // Calling the function using the function pointer
    int result = add_ptr(3, 4);
    printf("Result: %d\n", result);
 
    return 0;
}


Output

Result: 7

Previous Article
Next Article

Similar Reads

C - Pointer to Pointer (Double Pointer)
Prerequisite: Pointers in C The pointer to a pointer in C is used when we want to store the address of another pointer. The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers. We can use a pointer to a pointer to cha
5 min read
Declare a C/C++ function returning pointer to array of integer pointers
Declare "a function with argument of int* which returns pointer to an array of 4 integer pointers". At the first glance it may look complex, we can declare the required function with a series of decomposed statements. 1. We need, a function with argument int *, function(int *) 2. a function with argument int *, returning pointer to (*function(int *
2 min read
Difference between Dangling pointer and Void pointer
Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer: By deallocating memoryFunction CallWhen the variable goes out of scope Void pointer: Void pointer is a specific pointer type – void * – a pointer that point
2 min read
Pointer to an Array | Array Pointer
Prerequisite: Pointers Introduction  Consider the following program:  C/C++ Code #include&lt;stdio.h&gt; int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; printf(&quot;%p\n&quot;, ptr); return 0; } In the above program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer that can po
9 min read
How to Declare and Initialize an Array of Pointers to a Structure in C?
Prerequisite: Structure in CArray in C In C language, arrays are made to store similar types of data in contiguous memory locations. We can make arrays of either primitive data types, like int, char, or float, or user-defined data types like Structures and Unions. We can also make arrays of pointers in C language. Today we will learn how to create
8 min read
How to declare a Two Dimensional Array of pointers in C?
A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created following the way shown below. int *arr[5][5]; //creati
3 min read
How to Declare 3-Dimensional Arrays in Wiring in C?
Prerequisite: Array in C Arrays are used the make a list of the same data type elements and store it in contiguous memory locations. In simple words, arrays are the organization of elements of the same data types. There are many types for declaring the array i.e, 1-Dimensional arrays, 2-Dimensional arrays, and 3-Dimensional arrays. What is Wiring?
2 min read
Different ways to declare variable as constant in C
There are many different ways to make the variable as constant in C. Some of the popular ones are: Using const KeywordUsing MacrosUsing enum Keyword1. Using const KeywordThe const keyword specifies that a variable or object value is constant and can't be modified at the compilation time. Syntaxconst data_type variable_name = initial_value;ExampleTh
2 min read
Function Pointer in C
In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer. [GFGTABS] C++ #include &lt;stdio.h&gt; // A normal function with an int parameter // and void return type void fun(int a) { printf(&quot;Value of a is %d\n&quot;, a);
6 min read
Pointer vs Array in C
Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being: 1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2. the &amp; operator array is an alias for &amp;array[0
1 min read
Article Tags :
three90RightbarBannerImg