Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to a void function

What is the correct way for declaring a pointer to a void function?

void reverseArray(int arraySize, int a[]){
}

One of my many attempts which I think is somewhat correct

void (*reversePtr)(int, int) = &reverseArray;
like image 623
Horizon Avatar asked Oct 21 '25 22:10

Horizon


1 Answers

The second parameter of the function pointer has an invalid type. As the function is declared like

void reverseArray(int arraySize, int a[]);

then a pointer to the function will look ,like

void ( *reversePtr )( int, int[] ) = reverseArray;

or like

void ( *reversePtr )( int, int * ) = reverseArray;

Take into account that there is no need to use the address of operator in the initializer expression like &reverseArray. It is enough to write just reverseArray because a function designator used in expressions is converted to pointer to its type.

For example you could even write

void ( *reversePtr )( int, int * ) = *****reverseArray;

or

void ( *reversePtr )( int, int * ) = *&*&*&*&*&reverseArray;
like image 169
Vlad from Moscow Avatar answered Oct 23 '25 12:10

Vlad from Moscow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!