【问题标题】:When compiling a .c file, I get the following error: error: array type has incomplete element type编译 .c 文件时,出现以下错误:错误:数组类型具有不完整的元素类型
【发布时间】:2016-02-12 18:39:05
【问题描述】:

这是给我错误的行

struct label labelArray[100];

我觉得它的定义不正确。

如果有帮助,这里是其余代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const *argv[]) {
int curr_addr = 0;
int is_instruction = 0;

struct label labelArray[100];

while(1) {
    char* buffer = NULL;
    size_t size = 0;

    if (getline(&buffer, &size, stdin) == -1) {
        // end of file
        break;
    } else {
        char* line = buffer;
        if (line[0] == '\t') {
            // ignore first tab
            line++;
        }

        const char *ptr = strchr(line, ':');
        if (ptr) {
            int endlabel = ptr - line + 2;
            line += endlabel;
        }

...

【问题讨论】:

  • 其实我改变主意了。我投票关闭,但现在我投票重新开放。我认为不需要更多代码来演示此错误消息。其实by attempting to compile it the error message is reproduced exactly...
  • @Seb 如果这是唯一的代码,那么答案很简单。我们不知道这是否是唯一的代码。这可能是一个简单的错字,但如果没有 OP 正在使用的代码示例,我们就无法判断。
  • @Seb,问题是 OP 说 “这是行..” 而不是 “这是我的代码..”。所以我们知道有更多的代码,这肯定会让问题更清楚。
  • @Seb 即使发生错误的原因是未定义label,也可能是因为他们忘记了#include,没有完全相同的名称等。但是如果原因只是没有定义label,那么应该关闭它,因为解决方案可能没有帮助(“定义结构”)。

标签: c unix


【解决方案1】:

您从未定义过struct label。如果不先定义类型,就无法定义给定类型的变量。

struct label 的定义放在main 之前。然后您可以创建该类型的变量。

【讨论】:

    【解决方案2】:

    当你写这行时

    struct label labelArray[100];
    

    您正在声明一个结构数组,其中每个元素都是struct label 的精确副本。但要做到这一点,首先你需要声明struct label。您应该在main 之前声明它。

    如果您不能理解所有这些,简单来说,您正在尝试使用 struct 而不声明它。所以,就像你没有声明一个变量并使用它时,编译器会报错。

    另外,请参阅:Undefined Variable error

    【讨论】:

    • 该链接适用于使用extern 声明的变量,不适用于此处。
    猜你喜欢
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多