“sizeof的”不完全型'结构数组[]'无效申请 [英] invalid application of 'sizeof' to incomplete type 'struct array[]'

查看:166
本文介绍了“sizeof的”不完全型'结构数组[]'无效申请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过拆分来组织我的项目命令成更容易维护单独的文件。我遇到的问题是试图遍历在编译时定义的命令的数组。我创建了一个简单化的例子,再现了我得到的错误。

I am trying to organize my project by splitting commands up into separate files for easier maintenance. The issue I am having is trying to iterate over the array of commands defined at compile time. I have created a dumbed down example that reproduces the error I am getting.

.
├── CMakeLists.txt
├── commands
│   ├── CMakeLists.txt
│   ├── command.c
│   ├── command.h
│   ├── help_command.c
│   └── help_command.h
└── main.c

./的CMakeLists.txt

PROJECT(COMMAND_EXAMPLE)

SET(SRCS main.c)
ADD_SUBDIRECTORY(commands)

ADD_EXECUTABLE(test ${SRCS})

命令/的CMakeLists.txt

SET(SRCS ${SRCS} command.c help_command.c)

命令/ command.h

#ifndef COMMAND_H
#define COMMAND_H

struct command {
    char* name;
    int   (*init)(int argc, char** argv);
    int   (*exec)(void);
};

extern struct command command_table[];

#endif

命令/ COMMAND.C

#include "command.h"
#include "help_command.h"

struct command command_table[] = {
    {"help", help_init, help_exec},
};

命令/ help_command.h

#ifndef HELP_COMMAND_H
#define HELP_COMMAND_H

int help_command_init(int argc, char** argv);
int help_command_exec(void);

#endif

命令/ help_command.c

#include "help_command.h"

int help_command_init(int argc, char** argv)
{
    return 0;
}

int help_command_exec(void)
{
    return 0;
}

./的main.c

#include <stdio.h>
#include "commands/command.h"

int main(int argc, char** argv)
{
    printf("num of commands: %d\n", sizeof(command_table) / sizeof(command_table[0]));
    return 0;
}

如果您运行此

mkdir build && cd build && cmake .. && make

出现下列错误

path/to/main.c:6:40: error: invalid application of 'sizeof' to incomplete type 'struct command[]'

所以,我怎么遍历 command_table 如果我不能甚至决定命令的数量阵列中的?

So, how do I iterate over command_table if I can't even determine the number of commands in the array?

我知道有其他职位在那里与此相同的错误,但我花了一段时间,现在试图找出为什么这不起作用,继续失败:

I realize there are other posts out there with this same error, but I've spent a while now trying to figure out why this doesn't work and continue to fail:

推荐答案

有关你的的sizeof(command_table)工作,它需要看到这一点:

For your sizeof(command_table) to work, it needs to see this:

static struct command command_table[] = {
    {"help", help_init, help_exec},
};

但只看到这一点:

But it only sees this:

extern struct command command_table[];

眼看的sizeof()永远无法弄清楚有多少元素实际上是在那里。

Seeing that sizeof() can never figure out how many elements are actually in there.

顺便说一下,还有另外一个问题。 静态使得阵列中的所有其它模块可见。你必须将其删除或替代它。

Btw, there's another problem. static makes the array invisible in all other modules. You have to remove it or workaround it.

您选择(去除后静态)是:


  1. 硬编码要素的数量,例如

  1. hard-coding the number of elements, e.g.

的extern结构命令command_table [3];

限定一个额外的变量来保存的元素数:

defining an extra variable to hold the number of elements:

命令/ COMMAND.C

#include "command.h"
#include "help_command.h"

struct command command_table[] = {
    {"help", help_init, help_exec},
};

size_t command_count = sizeof(command_table)/sizeof(command_table[0]);

命令/ command.h

...
extern struct command command_table[];
extern size_t command_count;
...

然后你只需要使用 command_count

这篇关于“sizeof的”不完全型'结构数组[]'无效申请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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