0% found this document useful (0 votes)
11 views7 pages

Function Pointers in C 1722521727

The document explains function pointers in C, which are variables that store the address of functions, enabling dynamic function calls and flexible program design. It covers the declaration, definition, and various applications of function pointers, including callbacks, dynamic function selection, and state machines, with detailed code examples. Understanding function pointers enhances programming skills and allows for more versatile C programs.

Uploaded by

Divakar Sen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Function Pointers in C 1722521727

The document explains function pointers in C, which are variables that store the address of functions, enabling dynamic function calls and flexible program design. It covers the declaration, definition, and various applications of function pointers, including callbacks, dynamic function selection, and state machines, with detailed code examples. Understanding function pointers enhances programming skills and allows for more versatile C programs.

Uploaded by

Divakar Sen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

28_function_pointers_in_c.md https://embeddedforall.

com/ 2024-08-01

Function Pointers in C
Function pointers are an advanced feature in C programming that allow you to store the address of a function
in a pointer variable. They enable flexible and efficient code, especially useful for callbacks, dynamic function
calls, and implementing state machines. This blog post will cover the declaration, definition, and various uses
of function pointers, with detailed examples to illustrate each concept.

Table of Contents
1. What is a Function Pointer?
2. Declaring Function Pointers
3. Defining and Assigning Function Pointers
4. Using Function Pointers

What is a Function Pointer?


A function pointer is a variable that stores the address of a function. Unlike regular pointers that point to
data, function pointers point to executable code. This allows for dynamic function calls and flexible
program design.

Declaring Function Pointers

To declare a function pointer, you specify the return type, followed by an asterisk *, the pointer name, and the
parameter list enclosed in parentheses.

Syntax:

return_type (*pointer_name)(parameter_list);

Example:

int (*operation)(int, int);

This declares a pointer named operation that can point to any function taking two int parameters and
returning an int.

Defining and Assigning Function Pointers

After declaring a function pointer, you can assign it the address of a function using the function's name
without parentheses.

Example:

1/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01

#include <stdio.h>

// Function declaration
int add(int a, int b)
{
return a + b;
}

int main()
{
// Function pointer declaration
int (*operation)(int, int);

// Assigning the address of the function 'add' to the pointer


operation = add;

// Calling the function using the pointer


int result = operation(5, 3);
printf("Result: %d\n", result);

return 0;
}

output:

Result: 8

Using Function Pointers


Function pointers are particularly useful for implementing callbacks, dynamic function calls, and state
machines.

Callbacks

Callbacks are functions passed as arguments to other functions. They are commonly used in event-driven
programming and asynchronous operations.

Example:

#include <stdio.h>

// Callback function
void greet(const char *name)
{
printf("Hello, %s!\n", name);
}

// Function that accepts a callback

2/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01

void execute(void (*callback)(const char *), const char *arg)


{
callback(arg);
}

int main()
{
// Using the callback
execute(greet, "World");

return 0;
}

Dynamic Function Calls

Function pointers allow selecting and calling functions dynamically at runtime based on certain conditions.

Example:

#include <stdio.h>

int add(int a, int b)


{
return a + b;
}

int subtract(int a, int b)


{
return a - b;
}

int main()
{
// Function pointer declaration
int (*operation)(int, int) = NULL;
char op;

printf("Operation +/-: ");


scanf("%c", &op); // get the operation from user

// Assign function based on condition


if (op == '+')
{
operation = add;
}
else if (op == '-')
{
operation = subtract;
}
else
{

3/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01

printf("invalid operation");
}

// Call the selected function


if (operation != NULL)
{
int result = operation(5, 3);
printf("Result: %d\n", result);
}

return 0;
}

output:

Operation +/-: -
Result: 2
-----------
Operation +/-: +
Result: 8
-----------
Operation +/-: =
invalid operation

State Machines

State machines can be efficiently implemented using function pointers, making the code more modular and
maintainable.

Example:

#include <stdio.h>

// State functions
void stateA()
{
printf("In State A\n");
}

void stateB()
{
printf("In State B\n");
}

void stateC()
{
printf("In State C\n");
}

4/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01

int main()
{
// Array of function pointers
void (*state[])(void) = {stateA, stateB, stateC};

// Simulating state transitions


for (int i = 0; i < 3; i++)
{
state[i]();
}

return 0;
}

output:

In State A
In State B
In State C

Function Pointer Arrays

An array of function pointers allows storing multiple function addresses in a single data structure, which is
useful for implementing tables of functions.

Example:

#include <stdio.h>

// Function declarations
void add(int a, int b) {
printf("Add: %d\n", a + b);
}

void subtract(int a, int b) {


printf("Subtract: %d\n", a - b);
}

int main() {
// Array of function pointers
void (*operations[2])(int, int) = { add, subtract };

// Calling functions from the array


operations[0](5, 3);
operations[1](5, 3);

return 0;
}

5/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01

output:

Add: 8
Subtract: 2

Function Pointers in Structures

Storing function pointers in structures allows for more complex data structures, useful for object-oriented
programming patterns.

Example:

#include <stdio.h>

// Structure with function pointer


typedef struct
{
void (*operation)(int, int);
} Operation;

void add(int a, int b)


{
printf("Add: %d\n", a + b);
}

void subtract(int a, int b)


{
printf("Subtract: %d\n", a - b);
}

int main()
{
Operation op;

// Assigning and using the function pointer


op.operation = add;
op.operation(5, 3);

op.operation = subtract;
op.operation(5, 3);

return 0;
}

output:

Add: 8
Subtract: 2

6/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01

Function pointers in C are a powerful feature that allows for dynamic function calls, flexible program design,
and efficient code management. By understanding how to declare, define, and use function pointers, you can
enhance your programming skills and write more versatile C programs. Experiment with the examples
provided and explore advanced uses to master function pointers in C.

For more insightful content on Embedded Systems, follow:

Veeresh P S: https://www.linkedin.com/in/veeresh-p-s/
Embedded for All: https://embeddedforall.com/
Blog: https://embeddedforall.com/blog-2/
Youtube: https://www.youtube.com/@embeddedforallefa

7/7

You might also like