Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax of function pointers
3.
Declaration of a function pointer
3.1.
Syntax
3.2.
Example
4.
Calling a function
4.1.
Example
5.
Address of a function
5.1.
Example
6.
The Array of Function Pointers
6.1.
Example
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Function Pointer

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

A function pointer is a pointer that points to a function. They store the beginning address or entry point of the memory block, comprising all of the instructions of a function in extra detail. We use function pointers for several tasks. An example is to prevent code redundancy, which most programmers seek to achieve.

Also See: Sum of Digits in C and C Static Function

Syntax of function pointers

void fun()
void (*fun_ptr)(int);
fun_ptr = &fun;

We will consider function pointers like normal C functions where void is the function's return type. Here *fun_ptr is a pointer to a function that takes an int argument. It's as though we are declaring a function known as *fun_ptr, which takes int and returns void.

The key to writing the declaration for the function pointer is to think of it as a function declaration but with *fun_name in place of func_name. The pointer symbol * precedes the declaration of a function pointer because the function pointer can accept many parameters. It could point to any function that accepts two integer arguments and returns void.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Declaration of a function pointer

Syntax

return type (*ptr_name)(type1, type2…);  

Example

int (*ip) (int); 

In the above declaration, *ip is a pointer that points to a function that returns a value of type int and accepts an integer value as an argument.

float (*fp) (float);  

In the above declaration, *fp is a pointer that points to a function that returns a float value and accepts a float value as an argument.

We will observe that the declaration of a function is much like a function pointer except that the Pointer is preceded by a '*.' So, within the above declaration, fp is declared as a function rather than a function pointer.

Calling a function

By using a function pointer, we can also use the name of a function to call it. The syntax for calling the function through the function pointer is similar to calling it directly.

Example

include <iostream>  
using namespace std;  
void printname(char *name)  
{  
    std::cout << "Name:" <<name<< std::endl;  
}   
int main()  
{  
    char x[30];
    void (*ptr)(char*);
    ptr=printname; 
    std::cout << "Enter name: " << std::endl;  
    cin>>x;  
    cout<<x<<endl;  
    ptr(x); // calling printname() function  
   return 0;  
} 

Output

Enter name:
CodingNinjas
CodingNinjas
Name: CodingNinjas

In this example, we explain calling a function.

Address of a function

To get an address of a function, we need first to state the function's name. There is no want for us to call the function.

Example

#include <iostream>  
using namespace std;  
int main()  
{  
  std::cout << "The address of function main(): " <<&main<< std::endl;  
  return 0;  
}  

In the above program, we are displaying the address of our main function while listing the name of the function with no parameters no brackets to print the address of the main() function. As a result, a function's address is surely its name without any brackets or parameters.

You can also read about the dynamic arrays in c, And Tribonacci Series

The Array of Function Pointers

Function pointers are used in applications where we do not know which function could be called earlier. In an array of function pointers, the array takes the addresses of different functions, and the appropriate function could be called based on the index number.

Example

#include <stdio.h>
int sum(int num1, int num2);
int sub(int num1, int num2);
int mult(int num1, int num2);
int div(int num1, int num2);
int main() 
{  
  int x, y, choice, result;
  int (*ope[4])(int, int);
  ope[0] = sum;
  ope[1] = sub;
  ope[2] = mult;
  ope[3] = div;
  printf("Enter two integer numbers: ");
  scanf("%d%d", &x, &y);
  printf("Enter 0 to sum, 1 to subtract, 2 to multiply, or 3 to divide: ");
  scanf("%d", &choice);
  result = ope[choice](x, y);
  printf("%d", result);
return 0;}
int sum(int x, int y) {return(x + y);}
int sub(int x, int y) {return(x - y);}
int mult(int x, int y) {return(x * y);}
int div(int x, int y) {if (y != 0) return (x / y); 
else return 0;
}

Output

Enter two integer numbers: 13 48
Enter 0 to sum, 1 to subtract, 2 to multiply, or 3 to divide: 2
624


Also see,  Short int in C Programming

FAQs

  1. How to call a function without using the function name to send parameters?
    Here are two ways to pass parameters in C: Pass by Value, Pass by Reference.
    Pass by Value. Pass by Value, means that a copy of the data is made and stored by way of the name of the parameter. ...
    Pass by Reference. A reference parameter "refers" to the original data in the calling function.
     
  2. Which of the following is the correct syntax to pass a Function Pointer as an argument?
    void pass(int (*fptr)(int, float, char)){}

Key Takeaways

In this article, we have extensively discussed function Pointer topics, and we learned in this blog we've long gone through function pointers. We've seen the importance of function pointers, in which they're used, and how they make our programs simpler to develop and maintain. We can hope that this blog has helped you enhance your knowledge regarding the function of Pointer, and if you would like to learn more, check out our articles on Coding Ninjas and visit our Library for more. 
Check out this problem - Redundant Braces

Recommended Readings: 

Do upvote our blog to help other ninjas grow. 
"Happy Coding!"

Previous article
Array and pointers
Next article
Pointer Arithmetics
Live masterclass