Kalcifer
Kalcifer

Reputation: 1630

error: invalid application of 'sizeof' to incomplete type

In a header file, foo.h, I have something like:

extern const int array_foo[];

Then in a source file, foo.c, I have something like:

const int array_foo[] = {1 , 2, 3};

then in the main.c I have somehting like:

#include "foo.h"

int main(void)
{
    sizeof(array_foo);
    return 0;
}

However, compiling yields the error:

error: invalid application of 'sizeof' to incomplete type `const int[]`

I have it defined in the foo.c file, so why is the compiler complaining that it doesn't know what the array is?

Upvotes: 0

Views: 1273

Answers (1)

David Schwartz
David Schwartz

Reputation: 182789

You were supposed to put in foo.h all the information that main.c needs to know about array_foo. But you didn't put its size in there. That's fine so long as main.c doesn't need to know that. But it does, so that's not fine.

The header file must include whatever other files need to know. You could include a prototype of a get_size_of_array_foo function in foo.h, implement it in foo.c and call it in main.c if you want. But the size won't be known at compile time because you can't rely on the compiler being able to look into foo.c when it compiles main.c.

Upvotes: 1

Related Questions