C语言 C 中的结构:“params”的错误存储大小未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20056892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:20:32  来源:igfitidea点击:

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

cstruct

提问by mys.celeste

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

我对 C 有点陌生,并且在我目前正在进行的项目中遇到了一些麻烦。基本上我有以下文件:main.c、alarm.c、alarm.h

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

我在 alarm.c 中有一个结构声明,如下所示:

#define STRLEN 150;

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

In main.c I have:

在 main.c 我有:

#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:

在 alarm.h 我有:

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:

一般应该在头文件还是 c 文件中声明结构?或者它甚至重要吗?有什么不同:

struct foo {...};

and

typedef struct foo {...};

回答by ouah

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.cto declare an object of its type.

是不完整类型的声明。您可以创建指向此类型对象的指针,但在完成之前不能声明此类型的对象或获取其大小。您必须使其完整的声明可见main.c才能声明其类型的对象。

If you use the type in both alarm.cand main.c, just declare it in alarm.hand include alarm.hin main.c.

如果在alarm.cand中都使用该类型main.c,只需在 in 中声明alarm.h并包含alarm.h在 中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.

在后一种情况下,您还foo_为类型 name声明了一个别名struct foo

回答by Filipe Gon?alves

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

您必须在头文件中声明结构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?

此刻,当你include的时候,alarm.hmain里面的代码并没有看到结构组成,它看到的只是struct alarmparams;,所以不知道它是多长的。如何为不知道需要多少空间的东西分配空间?

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

typedef struct foo { ... };无效:typedef期望您为类型提供别名。typedef struct foo { ... } foo_t;会是正确的。

typedefis a storage class specifier, thus, you can think of it as any other regular declaration. Imagine you want an alias foofor the type bar: just declare a variable of type barand call it foo. Now, prepend a typedefkeyword 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 { ... };.

typedef是一个存储类说明符,因此,您可以将其视为任何其他常规声明。想象一下你想要一个foo类型的别名bar:只需声明一个类型的变量bar并调用它foo。现在,typedef在后面添加一个关键字,您就完成了。typedef通过这种简单的方法,即使是复杂的s 也可以很容易地理解。由于struct foo { ... };将是变量的无效声明(未提供名称),因此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.

通常,当您将在其他源文件中重用它们时,请在头文件中声明结构。如果您不打算这样做,在 .c 文件中声明它们应该没问题。

回答by jbat100

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

除了其他答案之外,STRLEN必须在编译时知道的值(在这种情况下很可能是已知的,但以防万一)。