0

I get this following error when I do this

"error: storage size of 'mscp_commands' isn't known"

struct command mscp_commands[]; /* forward declaration */

Later on I have:

struct command mscp_commands[] = {
 { "help",      cmd_help,       "show this list of commands"            },
 { "bd",        cmd_bd,         "display board"                         },
 { "ls",        cmd_list_moves, "list moves"                            },
 { "new",       cmd_new,        "new game"                              },
 { "go",        cmd_go,         "computer starts playing"               },
 { "test",      cmd_test,       "search (depth)"                        },
 { "quit",      cmd_quit,       "leave chess program"                   },
 { "sd",        cmd_set_depth,  "set maximum search depth (plies)"      },
 { "both",      cmd_both,       "computer plays both sides"             },
};

What's wrong with forward declaring the struct mscp_commands that way?

The command struct is defined earlier:

struct command {
    char *name;
    void (*cmd)(char*);
    char *help;
};
Flexo
  • 87,323
  • 22
  • 191
  • 272
user975900
  • 77
  • 4
  • 11

2 Answers2

7

struct command mscp_commands[]; is a definition not a declaration (assuming struct command is defined), but it doesn't know the storage size at that point, because the number of elements in mscp_commands isn't known. It's one of the cases where [] is notably different to *.

You could however write:

extern struct command mscp_commands[];

which would indeed be a declaration.

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • if I do that I get a LOT of "warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]" coming from struct command mscp_commands[] – user975900 Oct 14 '11 at 20:06
  • 3
    @user975900: That is because you canont bind a non-const char* to a string literal in c++. c++ is different to c. if you want to write c, maybe write c, not c++? – PlasmaHH Oct 14 '11 at 20:07
  • 3
    @user975900 you need to change your `char *` to `const char *`. – Mark Ransom Oct 14 '11 at 20:08
  • 2
    @user975900 - That's an error with your `struct command` - you need to say `const char *` for both `name` and `help` if you want to use string literals like that. – Flexo Oct 14 '11 at 20:08
1

With forward declarations the compiler cannot calculate the size of the object. Therefore the error message.

See also this link

tune2fs
  • 7,605
  • 5
  • 41
  • 57