Soup
Soup

Reputation: 23

Incompatible function pointer types when making array of pointers

I'm working on creating a basic shell in C, running into issues when compiling with gcc:

This is the warning from my IDE:

[query] incompatible function pointer types initializing 'const void (*)(char **)' with an expression of type 'void (*)(char **)' [-Wincompatible-function-pointer-types]

This is the error I'm getting when attempting make a pull request with a json test file set up:

 grsh.c:28:48: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]

   const void (*command_functions[]) (char **) = {&exit_shell, &change_directory, &set_path};
                                                  ^
  grsh.c:28:48: note: (near initialization for 'command_functions[0]')
  grsh.c:28:61: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
   const void (*command_functions[]) (char **) = {&exit_shell, &change_directory, &set_path};

Particular line of code in question:

const void (*command_functions[]) (char **) = {&exit_shell, &cd, &set_path};

I'll be 100%, not exactly sure what the issue here is. The program compiles and runs within Repl.it when running a normal compile command, but runs into issues when attempting to compile with -Werror and -Wall

Would love to get some feedback on how to properly initialize an array of pointers with type char.

Upvotes: 0

Views: 767

Answers (1)

dxiv
dxiv

Reputation: 17638

const void (*command_functions[]) (char **) = { /*...*/ };

The posted code copied above declares command_functions as an array of pointers to functions returning const void (which is legal, albeit uncommon, see for example this answer).

To declare command_functions as an array of const pointers to functions returning void use the following, instead.

void (*const command_functions[]) (char **) = { /*...*/ };

Upvotes: 2

Related Questions