Forward declaration of function pointer typedef

c, forward-declaration, function-pointers, typedef

Solution

Forward declare as you suggest:

/* Forward declare struct A. */
struct A;

/* Typedef for function pointer. */
typedef void (*func_t)(struct A*);

/* Fully define struct A. */
struct A
{
    func_t functionPointerTable[10];
};

For example:

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

struct A;

typedef void (*func_t)(struct A*);

struct A
{
    func_t functionPointerTable[10];
    int value;
};

void print_stdout(struct A* a)
{
    printf("stdout: %d\n", a->value);
}

void print_stderr(struct A* a)
{
    fprintf(stderr, "stderr: %d\n", a->value);
}

int main()
{
    struct A myA = { {print_stdout, print_stderr}, 4 };

    myA.functionPointerTable[0](&myA);
    myA.functionPointerTable[1](&myA);
    return 0;
}

Output:

stdout: 4
stderr: 4

See online demo http://ideone.com/PX880w .

As others have already mentioned it is possible to add:

typedef struct A struct_A;

prior to the function pointer `typedef` and full definition of `struct A` if it is preferable to omit the `struct` keyword.

Problem

I've run into a peculiar problem. It might be best to just show you what I'm trying to do and then explain it. ``` typedef void functionPointerType ( struct_A * sA ); typedef struct { functionPointerType ** functionPointerTable; }struct_A; ``` Basically, I have a structure `struct_A` with a pointer to a table of function pointers, who have a parameter of type `struct_A`. But I'm not sure how to get this compile, as I'm not sure how or if can forward declare this. Anyone know how this could be achieved? edit: minor fix in code

Original source