Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struct in C: Error storage size of 'params' isn't known

Tags:

c

struct

I'm a bit new to C and I'm having a bit of trouble with a project I'm currently working on. Essentially I have the following files: main.c, alarm.c, alarm.h

I have a struct declaration in alarm.c that looks like:

#define STRLEN 150;

struct alarmparams
{
    char time[STRLEN];
    char duration[STRLEN];
    char snooze[STRLEN];
    char port[STRLEN];
};

In main.c I have:

#include <stdio.h>
#include <string.h>
#include "alarm.h"

int main(int argc, char *argv[])
{   
    struct alarmparams params;

    printf("%s, %s\n", params.time, params.duration);
}

And in alarm.h I have:

struct alarmparams;

Right now when I go to compile I get the following error:

error: storage size of ‘params’ isn’t known

I've looked through other posts regarding this error, so I have done a bit of research on this already. I've also tried some of the suggested solutions and it's either I get the exact same error or I got more on top of it. I'm at a lose as to how to fix this.

Is there something I'm missing? Did I declare something incorrectly?

In general should structs be declared in the header file or the c file? Or does it even matter? What's the different between:

struct foo {...};

and

typedef struct foo {...};
like image 392
mys.celeste Avatar asked Nov 18 '13 20:11

mys.celeste


2 Answers

struct alarmparams;

is the declaration of an incomplete type. You can create a pointer to an object of this type but you cannot declare an object of this type or take its size until it has been completed. You have to make its complete declaration visible in main.c to declare an object of its type.

If you use the type in both alarm.c and main.c, just declare it in alarm.h and include alarm.h in main.c.

For you second question, the difference between:

struct foo {...};

and

typedef struct foo {...} foo_;

is in the latter case you also declare an alias foo_ for the type name struct foo.

like image 105
ouah Avatar answered Nov 10 '22 00:11

ouah


You have to declare the structure in the header file alarm.h.

At the moment, when you include alarm.h, the code in main doesn't see the structure composition, all it sees is struct alarmparams;, so it doesn't know how long it is. How can you allocate space for something that you don't know how much space it takes?

typedef struct foo { ... }; is invalid: typedef expects you to provide an alias for a type. typedef struct foo { ... } foo_t; would be correct.

typedef is a storage class specifier, thus, you can think of it as any other regular declaration. Imagine you want an alias foo for the type bar: just declare a variable of type bar and call it foo. Now, prepend a typedef keyword behind, and you are done. Even complicated typedefs can be easily understood with this simple approach. Since struct foo { ... }; would be an invalid declaration for a variable (no name is provided), so is typedef struct foo { ... };.

In general, declare structures in the header file when you will reuse them in other source files. If you don't plan on doing so, declaring them on the .c file should be fine.

like image 31
Filipe Gonçalves Avatar answered Nov 09 '22 22:11

Filipe Gonçalves