Matrix function pointer problem.

I've got this code...
1
2
3
4
5
6
7
8
9
#define n_event 2
#define n_state 3
struct event {
... //Some info here. not really matter.
};

typedef void(*p_fun_v)(event*);
typedef p_fun_v matrix[n_state][n_event];
typedef p_fun_v *(p_matrix[n_state][n_event]);


i (should) have declared 3 new type:
The first should be a pointer to a function that return void and has event pointer as argoument.
The second is a matrix of the previous pointer and the third is a pointer to that matrix.

Then, i write.
 
static p_matrix p_event = new matrix {{0}};


And the compiler returns the error "array must be initialized with a brace-enclosed initializer"

same error without initializing it to 0.

I tried also with this:
 
static matrix *p_event = new matrix;

and the compiler return "cannot convert ‘void (* (*)[2])(event*)’ to ‘void (* (*)[3][2])(event*)’ in initialization" (tried with and without inizializing.

I'm really lost. i searched for 1 hour all over internet, then i posted here.
Thanks in advance, nice forum anyway!

Summary:

I want to define a pointer to a matrix of function pointers.
Last edited on
p_matrix is not a pointer. It is an array of pointers to a function pointer. The record

typedef p_fun_v *(p_matrix[n_state][n_event]);

is equivalent to

typedef p_fun_v * p_matrix[n_state][n_event] ;

that is the parentheses in this record have no any sense.

You should write

typedef p_fun_v ( *p_matrix )[n_state][n_event];

Also I am not sure that you indeed need a pointer to that matrix. Maybe you need a pointer to the first element of the matrix?
Last edited on
I corrected the typedef as you said, but the compiler still complains:

cannot convert ‘void (* (*)[2])(event*)’ to ‘void (* (**)[3][2])(event*)’ in initialization


in line:

static p_matrix p_event = new matrix {{0}};

Thanks for your answer, i really look forward to solve this.

EDIT:

I'm not sure myself really... i'll go OT for a simple question.
I'm doing this to avoid to put switches and if when i call function (so it should be faster).
I have a menu application, in which i want to manage various state (an enum takes care of it) and various
input from the player. There are a lot of switches to do if i don't use arrays of pointer, so for me it shoudl be "worth".
This is my first real "worthy" project, but i'm not that skilled as programmer.
In your opinion, is it good this way? or i should put switches? (maybe the compiler is already optimized for that kind of routine...).
Last edited on
I said already that you need no a pointer to the matrix.

typedef void(*p_fun_v)(event*);
typedef p_fun_v matrix[n_state][n_event];
typedef p_fun_v ( *p_matrix )[n_event];

Topic archived. No new replies allowed.