C 中的结构:“params"的错误存储大小未知 [英] struct in C: Error storage size of 'params' isn't known

查看:23
本文介绍了C 中的结构:“params"的错误存储大小未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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];
};

在 main.c 中我有:

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);
}

在 alarm.h 我有:

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?

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

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

typedef struct foo {...};

推荐答案

struct alarmparams;

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

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.

如果在alarm.cmain.c中都使用该类型,只需在alarm.h中声明并包含main.c 中的 >alarm.h.

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

typedef struct foo {...} foo_;

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

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

这篇关于C 中的结构:“params"的错误存储大小未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆