jethrob
jethrob

Reputation: 1

variable has incomplete type 'struct stat'

Hi trying to port some code and I'm not understanding the problem here,

getting the error - file.c:370:17: error: variable has incomplete type 'struct stat'

here is the code that is throwing up the error

int srcfd, destfd;
int nread;
char ifc[PROPERTY_VALUE_MAX];
char *pbuf;
char *sptr;
struct stat sb;

if (stat(config_file, &sb) != 0)
    return -1;

pbuf = malloc(sb.st_size + PROPERTY_VALUE_MAX);
if (!pbuf)
    return 0;

i've read various stackoverflow questions regarding incomplete types and defining in header files, but i don't understand how to implement any of that. i've tried moving the function about, declaring it in a header file and outside the function . but get the same error.

Upvotes: 0

Views: 2517

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

The structure struct stat is only declared but not defined.

struct stat sb;

So the compiler does not know how much memory an object of the structure type will require and whether indeed the structure has for example the data member st_size used in this statement

pbuf = malloc(sb.st_size + PROPERTY_VALUE_MAX);
              ^^^^^^^^^^

If the structure is defined in some header then you need to include the header in this translation unit.

Upvotes: 1

Related Questions