struct in C: Error storage size of 'params'

2019-06-19 01:29发布

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 {...};

标签: c struct
3条回答
你好瞎i
2楼-- · 2019-06-19 02:06

In addition to the other answers, the value of STRLEN must be known at compile time (it most likely is in this case, but just in case).

查看更多
Emotional °昔
3楼-- · 2019-06-19 02:11
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.

查看更多
我想做一个坏孩纸
4楼-- · 2019-06-19 02:23

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.

查看更多
登录 后发表回答