Invalid 'sizeof' application for incomplete type 'array array []'

I am trying to organize my project, breaking commands into separate files for simplification of service. The problem I am facing is trying to iterate over an array of commands defined at compile time. I created a heady example that reproduces the error I get.

. β”œβ”€β”€ CMakeLists.txt β”œβ”€β”€ commands β”‚  β”œβ”€β”€ CMakeLists.txt β”‚  β”œβ”€β”€ command.c β”‚  β”œβ”€β”€ command.h β”‚  β”œβ”€β”€ help_command.c β”‚  └── help_command.h └── main.c 

./CMakeLists.txt

 PROJECT(COMMAND_EXAMPLE) SET(SRCS main.c) ADD_SUBDIRECTORY(commands) ADD_EXECUTABLE(test ${SRCS}) 

Commands /CMakeLists.txt

 SET(SRCS ${SRCS} command.c help_command.c) 

Commands /command.h

 #ifndef COMMAND_H #define COMMAND_H struct command { char* name; int (*init)(int argc, char** argv); int (*exec)(void); }; extern struct command command_table[]; #endif 

Commands /command.c

 #include "command.h" #include "help_command.h" struct command command_table[] = { {"help", help_init, help_exec}, }; 

Commands / help _command.h

 #ifndef HELP_COMMAND_H #define HELP_COMMAND_H int help_command_init(int argc, char** argv); int help_command_exec(void); #endif 

Commands / help _command.c

 #include "help_command.h" int help_command_init(int argc, char** argv) { return 0; } int help_command_exec(void) { return 0; } 

./main.c

 #include <stdio.h> #include "commands/command.h" int main(int argc, char** argv) { printf("num of commands: %d\n", sizeof(command_table) / sizeof(command_table[0])); return 0; } 

If you run this

 mkdir build && cd build && cmake .. && make 

following error occurs

 path/to/main.c:6:40: error: invalid application of 'sizeof' to incomplete type 'struct command[]' 

So, how can I command_table over command_table if I can't even determine the number of commands in the array?

I understand that there are other posts with the same error, but I spent some time trying to understand why this is not working and continues to fail:

+4
source share
2 answers

For your sizeof(command_table) work, it should see this:

 static struct command command_table[] = { {"help", help_init, help_exec}, }; 

But he sees only this:

 extern struct command command_table[]; 

Seeing that sizeof() never figure out how many elements are actually there.

Btw, there is another problem. static makes the array invisible in all other modules. You must delete it or bypass.

Your options (after removing static ):

  • hard coding of the number of elements, for example

    extern struct command command_table[3];

  • definition of an additional variable to store the number of elements:

Commands /command.c

 #include "command.h" #include "help_command.h" struct command command_table[] = { {"help", help_init, help_exec}, }; size_t command_count = sizeof(command_table)/sizeof(command_table[0]); 

Commands /command.h

 ... extern struct command command_table[]; extern size_t command_count; ... 

And then you just use command_count .

+12
source

Explain the number of elements in your array in commands/command.h :

 extern struct command command_table[3]; 
+1
source

Source: https://habr.com/ru/post/1439542/


All Articles