1

Let's say I have an array of arrays of function pointers. In other words, I might want to call a matrix transpose function like so, depending upon what dtype my matrix is:

Transp[dtype][index_dtype](A.ia, A.a, B.ia, B.a);

Functions in Transp might look like this:

void transp_i64_i8(const int64_t* ia, const int8_t* a, int64_t* ib, int8_t* b) {
  // transpose here
  return;
}

except varying the pointer types.

It seems to me that I should declare my function pointer array like so:

void (**Transp)(const void* ia, const void* a, const void* ib, const void* b)[DTYPES_MAX][INDEX_TYPES_MAX] = {
  {transp_i8_i8, transp_i8_i16, transp_i8_i32, /* ... */ },
  {transp_i16_i8, transp_i16_i16, /* ... */ },
  {transp_i32_i8, transp_i32_i16, /* ... */ },
  /* ... */
}

Unfortunately this doesn't seem to work:

error: called object ‘Transp[(int)self_m->storage->dtype][(int)((struct YALE_STORAGE *)self_m->storage)->index_dtype]’ is not a function
../../../../ext/nmatrix/nmatrix.c: In function ‘nm_complex_conjugate_bang’:
../../../../ext/nmatrix/nmatrix.c:1910:32: error: subscripted value is neither array nor pointer nor vector

I found one fairly useful reference, but I really need an example for my exact use-case to understand and apply.

So what, exactly, is the correct way to define an array of arrays of function pointers? Specifically, how is the declaration portion written?

(I realize this can be done with a typedef much more easily, but I'm writing a code generator, and would rather not use a typedef.)

1
  • 1
    Why does a code generator preclude the use of typedefs? Just because the code is auto-generated, it doesn't mean you aren't going to want to read it from time to time. Commented Jun 15, 2012 at 21:09

3 Answers 3

3

You declare it in a similar way to how you would use it, e.g.:

void (*Transp[DTYPES_MAX][INDEX_TYPES_MAX])(const int64_t*,
                                            const int64_t*,
                                            const int64_t*,
                                            const int64_t*);
Sign up to request clarification or add additional context in comments.

1 Comment

@mohawkjohn: No, it's an array of arrays of pointers to functions. Just because the array is two dimensional doesn't meant you need extra indirections in the array element itself.
2

So the type of a function pointer is

ReturnType (*)(Args);

The type of an array of function pointers is

ReturnType (*[n])(Args);

So an array of arrays of function pointers would be

ReturnType (*[n][m])(Args);

Comparing this to what you have, it looks like you're declaring your array as

ReturnType (**)(Args)[n][m];

That is, a pointer to a pointer to a function that returns an array of arrays. If you remove one of the stars from your variable declaration and move the arrays inside the parentheses, I think this will resolve the problem.

Hope this helps!

4 Comments

@Mehrdad- I don't think you can declare an array of functions in C or in C++.
Your args are in the wrong place, array of array of function pointers vs pointer to function returning an array of arrays.
@CharlesBailey- I just caught that. Can you double-check this revised version is correct?
@templatetypedef: Then I guess I was wrong... I know you can certainly pass a single one as a parameter so I was guessing maybe you can pass arrays of them, but if you can't then never mind. :)
0

If your function pointers all have a fixed number of parameters but different number of parameters or different parameter types, you cannot anyway portably use a single type of function pointers to represent all the function pointers.

In POSIX system you can use an array of array of void * as POSIX guarantees the representation of function pointers and void * is the same. In C actually there is no conversion between function pointers and void * but compilers usually support it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.