Search notes:

C: function pointers

Three functions with the same signature

Here are three functions. All of the have the same signature: they take a char const* and a char and they return an unsigned int:
unsigned int count(char const *text, char c);
unsigned int first(char const *text, char c);
unsigned int last (char const *text, char c);
Github repository about-c, path: /datatypes/pointer/function/funcs.h
Here is their implementation:
unsigned int count(char const *text, char c) {

  unsigned int returnValue = 0;

  for (const char* t=text; *t; t++) {

    if (*t == c) returnValue ++;

  }

  return returnValue;
}

unsigned int first(char const *text, char c) {

  unsigned int returnValue = 0;

  for (const char* t=text; *t; t++) {

    if (*t == c) return returnValue;

    returnValue ++;

  }

  return -1;
}

unsigned int last(char const *text, char c) {

  unsigned int returnValue = -1;

  for (const char* t=text; *t; t++) {

    if (*t == c) returnValue = t-text;

  }

  return returnValue;
}
Github repository about-c, path: /datatypes/pointer/function/funcs.c

Using a function pointer

The following short code declares a function pointer, assigns a function to it and executes the function via the function pointer
#include <stdio.h>
#include "funcs.h"


int main() {

 //
 // Declare a function pointer:
 // 
    unsigned int (*funcPtr) (char const*, char);

 //
 // Assign a function to the function pointer
 //
    funcPtr = &first;

 //
 // Execute function pointer
 //
    printf("%d\n", (*funcPtr)("Hello world", 'o'));

}
Github repository about-c, path: /datatypes/pointer/function/declare-assign-execute.c

Passing a function pointer to a function

A function whose first argument is a function pointer. The function executes the function pointer with the other two arguments it receives:
#include <stdio.h>
#include "funcs.h"

int execute_funcPtr(unsigned int (*funcPtr)(char const*, char), char const* arg_1, char arg_2) {

   printf("funcPtr returned %d\n", (*funcPtr)(arg_1, arg_2));

}


int main() {

  execute_funcPtr(&count, "Hello world", 'o');
  execute_funcPtr(&first, "Hello world", 'o');
  execute_funcPtr(&last , "Hello world", 'o');

}
Github repository about-c, path: /datatypes/pointer/function/pass-to-func.c

Returning a function pointer

Similarly, it's possible to declare a function that returns a function pointer:
#include <stdio.h>
#include "funcs.h"

#define FUNC_COUNT 1
#define FUNC_FIRST 2
#define FUNC_LAST  3

unsigned int (*getFuncPointer(int funcNo))(char const*, char) {

  if (funcNo == FUNC_COUNT) return &count;
  if (funcNo == FUNC_FIRST) return &first;
  if (funcNo == FUNC_LAST ) return &last ;
  return NULL;

}


int main() {

    unsigned int (*funcPtr) (char const*, char);

    funcPtr = getFuncPointer(FUNC_LAST);

    printf("%d\n", (*funcPtr)("Hello world", 'o'));

}
Github repository about-c, path: /datatypes/pointer/function/return.c

See also

Typedef'fing a function pointer
The abstract .NET class System.Delegate.
The method GetDelegateForFunctionPointer of the .NET class System.Runtime.InteropServices.Marshal.

Index