I can not figure out how to find the size of the array of pointers to a function.

int show(void(*f[])(int _x)) { return sizeof((*f[])(0)) / sizeof((*f)(0))); } 
  • 2
    No In C, pass the size explicitly; in C ++ you can use standard containers. - PinkTux
  • one
    The question has nothing to do with function pointers. - AnT

2 answers 2

For such a function declaration

 int show(void(*f[])(int _x)) { return sizeof((*f[])(0)) / sizeof((*f)(0))); } 

You can not determine the size of the array, because the function parameter, declared as an array, is implicitly reduced to the type of the pointer to the type of the array element. Accordingly, the array passed to the function as an argument is also implicitly converted to a pointer to its first element.

Therefore, the function declaration looks like this.

 int show(void(**f)(int _x)) { return sizeof((*f[])(0)) / sizeof((*f)(0))); } 

That is, the function parameter is a pointer to a function pointer.

The same expression in the function body is in any case incorrect.

In C, you should also pass the size of the array to the function. In C ++, you could declare a parameter as a reference to an array.

Here is an example of a C ++ demonstration program that shows two approaches: how to define a function in C and C ++, setting another function for the function corresponding to the number of elements in the array, and how you can define a function (only) in C ++, passing an array of pointers to functions by reference.

 #include <iostream> void f(int) { std::cout << "f" << std::endl; } void g(int) { std::cout << "g" << std::endl; } int h(void(**fp)(int), size_t n ) { for ( size_t i = 0; i < n; i++ ) fp[i]( 0 ); return 0; } template <size_t N> int h( void ( *( &fp )[N] )( int ) ) { for (size_t i = 0; i < N; i++) fp[i](0); return 0; } /* using FP = void ( * )( int ); template <size_t N> int h( FP ( &fp)[N] ) { for (size_t i = 0; i < N; i++) fp[i](0); return 0; } */ int main() { void ( *fp[] )( int ) = { f, g }; h(fp, sizeof( fp ) / sizeof( *fp ) ); h(fp); return 0; } 

Output of the program to the console

 f g f g 

The commented code shows how you can use the using declaration to make the declaration of the reference template parameter of a function easier.

As can be seen from the program, if you declare a parameter as a link to an array, then it is easy to get the size of the argument array. It is equal to the template parameter N

Here is a C demo.

 #include <stdio.h> void f(int x ) { printf( "f(%d)\n", x ); } void g(int x ) { printf( "g(%d)\n", x ); } int h(void(**fp)(int), size_t n ) { for ( size_t i = 0; i < n; i++ ) fp[i]( i ); return 0; } int main(void) { void ( *fp[] )( int ) = { f, g }; h(fp, sizeof( fp ) / sizeof( *fp ) ); return 0; } 

    In this case - no way.

    You are in the function where the address of the beginning of the array is passed. Information about its size remained in the place of its definition (where memory was allocated for it).

    Here there sizeof(f) / sizeof(f[0]) works and this expression can be memorized and passed to a function along with an array.

    Something like this

     void do_it (int (*f[])(), size_t asize) { printf("array %zd items\n", asize); } int main (int ac, char *av[]) { int (*f[])() = {f1, f2, f3}; printf("%ld\n", sizeof(f) / sizeof(f[0])); do_it(f, sizeof(f) / sizeof(f[0])); }