Typedef function pointer forward declaration

I had a peculiar problem. It’s 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_Awith a pointer to a table of function pointers that have a type parameter struct_A. But I'm not sure how to get this compilation, since I'm not sure how or if you can forward an announcement about it.

Does anyone know how this can be achieved?

edit: minor bug fix in code

+5
source share
3 answers

Go ahead and 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 instance:

#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;
}

Conclusion:

stdout: 4
stderr: 4

Watch the online demo at http://ideone.com/PX880w .


, :

typedef struct A struct_A;

typedef struct A, struct.

+9

, , :

//forward declaration of the struct
struct _struct_A;                               

//typedef so that we can refer to the struct without the struct keyword
typedef struct _struct_A struct_A;              

//which we do immediately to typedef the function pointer
typedef void functionPointerType(struct_A *sA); 

//and now we can fully define the struct    
struct _struct_A                        
{
    functionPointerType ** functionPointerTable;
};
+1

:

typedef struct struct_A_
{
    void  (** functionPointerTable) (struct struct_A_);
}struct_A;


 void typedef functionPointerType ( struct_A ); 
0
source

All Articles