2

How would I declare a as a array of 4 pointers to functions with no parameters and which return void? The pointers originally point to functions with names: insert, search, update, and print.

This is the closest I can get to the declaration:

void (*a[4]) () {insert,search,update,print}

1
  • 1
    You might want to start with typedef void (*func)(void); and then create an array of func.
    – BoP
    May 23 at 19:10

6 Answers 6

4

It's so much more readable using a typedef when dealing with function pointers.

typedef void (*MyFuncPtr)( void );

MyFuncPtr a[] = { insert, search, update, print };

Note that no arguments is denoted using (void), not ().

Note that while [4] is ok, [] is sufficient here.

Without, it would be

void (*a[])( void ) = { insert, search, update, print };
2

In ISO C18, the line

void (*a[4]) ();

will declare an array of 4 elements in which each element is a pointer to a function returning void that takes an unspecified number of arguments. If you want to specify that the functions to take no arguments, then you must write (void) instead of ():

void (*a[4])(void);

However, in the upcoming ISO C23, this is no longer necessary, as specifying a function with an unspecified number of arguments is no longer possible (except for variadic functions). In C23, it is therefore sufficient to write () instead of (void). This also applies to ISO C++.

Also, if you want to initialize the array, you must use =, like this:

void (*a[4])(void) = {insert,search,update,print};

In C++, the = is not necessary during initialization, but it is necessary in C (both in C18 and C23).

2
  • So the C++ counterpart would be same as OP's?
    – Haris
    May 23 at 19:15
  • @Haris: Yes, OP's original code is correct in modern C++. May 23 at 19:54
1

You're missing the assignment operator. Change it to:

// Thee may dropeth the 4, shouldst thee so wanteth.
void (*a[4])(void) = { insert , search , update , print };

Note that empty brackets specify an unspecified number of arguments (not necessarily 0). If that's not what you want, specify void.

0

To add a more step-by-step way of thinking about this, a good way to think about complex C declarations is that they are declared the same way they are used. So ask yourself how you would use (index and call) an array of function pointers?

  • First we would index a[]
  • Then dereference the function pointer to obtain a function *a[]
  • And then we would call (*a[])()

Now to turn this into a declaration:

  • Since the functions return void and take no arguments the declaration is void and add void to the parameters bringing us to void (*a[])(void);
  • Finally add an initializer list = {} as we would a normal array

This leaves us with:

void (*a[])(void) = {insert, search, update, print};
0

Alternatively, one can use a popular typeof extension (a feature in C23) for convenient and readable syntax.

typeof(void(void)) * a[] = {...};

The a is an array of pointers to void(void) function.

0

Developers usually not using function pointers. These reasons are:

  • A switch statement, so letting the compiler do the work is preferable.

  • Function pointers are dangerous.

  • Function pointers are too difficult to code and maintain. but you can use:

    return_type (*function_array[4])(argument_list);
    
2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 29 at 20:47
  • You should probably read this: kernel.org/doc/html/next/filesystems/vfs.html Every Linux filesystem is implemented using function pointers. Other OSes are the same. SIgnal handlers in C are installed via a function pointer. Multithreading is implemented using function pointers. Sorting such as standard C qsort() uses function pointers. Jump tables in C use function pointers, and can make for easy-to-maintain code. Jun 4 at 22:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.