Simple C Function Pointer Example

Here is a simple function pointer example I wrote for one of my friends just learning to program in C - it should be easy to follow:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef void (*ledFuncPtr) (int[], int);

void myFunctionTest(int pins[], int size)
{

        int i = 0;
        for (i = 0; i <= size; i++) {
                printf("val: %d\n", pins[i]);
        }
}

static void led_seq_test(int pins[], int pin_arr_size, ledFuncPtr function)
{
        function(pins, pin_arr_size);
}

int main(int argc, char **argv)
{

        int green_pins[] = { 1, 2, 3 };
        size_t size = sizeof(&green_pins) / sizeof(int);

        printf("size: %ld\n", size);

        led_seq_test(green_pins, size, &myFunctionTest);

        return (0);
}

Blog tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <python> <c>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.