Help w/ "incomplete type" error

K

kj

Hi. I'm trying to compile some software from source, and I'm
getting an error I can't figure out. The error in question is

key_events.h:38: field `id' has incomplete type

and the lines corresponding to the error are:

struct kb_event_id_s
{
char name[16];
int number;
};
typedef struct kb_event_id_s kb_event_id_t;

struct kb_event_map_s {
char prefix[8];
const kb_event_id_t id[];
};
typedef struct kb_event_map_s kb_event_map_t;


I can't spot the error. Can someone give me a hand?

Thanks!

kj
 
R

Richard Bos

kj said:
Hi. I'm trying to compile some software from source, and I'm
getting an error I can't figure out. The error in question is

key_events.h:38: field `id' has incomplete type
struct kb_event_map_s {
char prefix[8];
const kb_event_id_t id[];
};
I can't spot the error.

The error is precisely what the error message told you: the field called
id, in the struct called kb_event_map_s, has incomplete type. Variable-
length arrays are valid only in C99, and you're probably using a C89
compiler, so you can't do this. You must give id some specific size. If
you need a variable-length array, you can either find a C99 compiler, or
make id a pointer and use malloc().

Richard
 
S

Suman

kj said:
Hi. I'm trying to compile some software from source, and I'm
getting an error I can't figure out. The error in question is

key_events.h:38: field `id' has incomplete type

and the lines corresponding to the error are:

struct kb_event_id_s
{
char name[16];
int number;
};
typedef struct kb_event_id_s kb_event_id_t;

struct kb_event_map_s {
char prefix[8];
const kb_event_id_t id[];
^^
The error is due to the fact that you have not specified
the number of elements that id can/should hold. The compiler is at a
loss.
Either initialise the kb_event_id_t array with specific
objects or specify some value between [],
that is evaluated to some integer at compile time. Of course there
are VLA's in C99 but the above should be good enough to get started.
 
M

Me

key_events.h:38: field `id' has incomplete type
struct kb_event_map_s {
char prefix[8];
const kb_event_id_t id[];
};


I can't spot the error. Can someone give me a hand?

Your compiler doesn't support C99's flexible array members. Some
pre-C99 compilers implemented this as id[0], if that fails you can
always try the "struct hack" trick (not legal but works well in
practice).
 
D

Dave Thompson

kj wrote:
key_events.h:38: field `id' has incomplete type
struct kb_event_map_s {
char prefix[8];
const kb_event_id_t id[];
^^
The error is due to the fact that you have not specified
the number of elements that id can/should hold. The compiler is at a
loss.

For a C90 compiler, as others have noted, true.
Either initialise the kb_event_id_t array with specific
objects or specify some value between [],
that is evaluated to some integer at compile time. Of course there
are VLA's in C99 but the above should be good enough to get started.

All three of these are only applicable to a 'top-level' object, not a
field in a struct which is obviously the case here and as the OP's
quoted error message confirms.

- David.Thompson1 at worldnet.att.net
 
S

Suman

Dave Thompson wrote:
[snip]...
Either initialise the kb_event_id_t array with specific
objects or specify some value between [],
that is evaluated to some integer at compile time. Of course there
are VLA's in C99 but the above should be good enough to get started.

All three of these are only applicable to a 'top-level' object, not a
field in a struct which is obviously the case here and as the OP's
quoted error message confirms.
My mistake.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top