【问题标题】:C - error: expected identifier or '(' before 'void'C - 错误:预期标识符或 '(' 在 'void' 之前
【发布时间】:2016-04-30 23:41:01
【问题描述】:

我正在学习 C,并试图创建一个 ArrayList 以供练习。当我尝试编译这段代码时,我得到了很多错误,但我不知道为什么会这样:

Test.c:12:13: 错误:预期标识符或 '(' 在 'void' 结构初始化(无效);

Google 上没有太多关于这个与 void 相关的错误,因此我们将不胜感激任何帮助。谢谢!

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

    struct ArrayList {
    int size;
    int typeSize;
    int index;
    int array[];
};

struct init(void);

void add(struct list, int x);

int get(struct list, int x);


int main() {
   struct ArrayList list = init();
   add(list, 4);
   printf("%d", get(list, 0));
   return 0;
}


struct init(void) {
    ArrayList this;
    this.size = 0;
    this.index = 0;
    return this;
}

void add(struct list, int x) {
    list->array[size] = x;
    size++;
}

int get(struct list, int index) {
    return list->array[index];
}

【问题讨论】:

  • 顺便说一句 list-&gt;array[size] = x; :区域尚未得到保护。
  • 并将struct list替换为struct ArrayList *list

标签: c


【解决方案1】:
struct init(void);

这是一个语法错误。

给定类型声明struct ArrayList { ... },类型的名称是struct ArrayList。如果init 应该返回一个ArrayList 结构,则需要声明为:

struct ArrayList init(void);

init 函数的定义也有错误。本声明:

ArrayList this;

需要:

struct ArrayList this;

(作为风格点,您可能会考虑使用 this 以外的名称。在 C++ 中,this 是一个关键字,而您将其用作标识符的方式与其 C++ 含义不一致。那就是完全合法,但可能会引起一些混乱。我建议称它为result。)

【讨论】:

    【解决方案2】:

    结构名称丢失。

    struct ArrayList init(void);
    
    struct ArrayList init(void) {
        ArrayList this;
        this.size = 0;
        this.index = 0;
        return this;
    }
    

    另一个问题,array 在您的情况下没有初始化,在addget 中使用它是未定义的行为:

    struct ArrayList init(void) {
        ArrayList this;
        this.size = 0;
        this.index = 0;
        return this;
    }
    

    在这种情况下,灵活数组成员array 不与任何存储关联,最好这样做:

    struct ArrayList* init(void) {
        ArrayList *this = malloc(sizeof(struct ArrayList) + sizeof(int[NUM]));
        this->size = NUM;
        this->index = 0;
        return this;
    }
    

    addget 函数也需要修复:

    void add(struct ArrayList *list, int x) {
        list->array[list->index++] = x;
    }
    
    int get(struct ArrayList *list, int index) {
        return list->array[index];
    }
    

    【讨论】:

    • this.array = malloc(SIZE * sizeof(int)); 无法编译。 this.array 是灵活数组,不是指针。
    • @BLUEPIXY 你是对的,整个结构需要分配数组的存储空间,struct ArrayList list * = malloc(sizeof(struct ArrayList) + sizeof (int[size])); 但 OP 的 init 返回一个结构,而不是指针,这会使这变得困难.
    • 可能,this.size = 0; --> this-&gt;size = NUM;list-&gt;array[size] = x; --> list-&gt;array[list-&gt;index++] = x; 并且需要范围检查和扩展。
    • 已修复,复制粘贴该部分 :(
    • 当然,它们(范围检查和扩展)只是被提及。你不执行。
    【解决方案3】:
    struct init(void);
    

    struct 本身不是一个足够的类型。你可能想改用这样的东西:

    struct ArrayList init(void);
    

    【讨论】:

      【解决方案4】:

      struct init(void);——它看起来像一个没有参数的函数,它返回一个struct。但是struct 是什么?应该是struct ArrayList

      对于没有 struct 类型名称的 struct 的其余外观相同。

      【讨论】:

        【解决方案5】:

        你的数据类型有很多错误..结构不是数据类型。检查这个。我已经更改了您的代码。请记住,如果您要使用指针,您至少应该传递地址

        #include <stdio.h>
        #include <stdlib.h>
        
        struct ArrayList {
        int size;
        int typeSize;
        int index;
        int *array;
        };
        
        struct ArrayList init();
        
        void add(struct ArrayList* list, int x);
        
        int get(struct ArrayList* list, int x);
        
        
        int main() {
           struct ArrayList list = init();
           add(&list, 4);
           printf("%d", get(&list, 0));
           return 0;
        }
        
        
        struct ArrayList init() {
           struct ArrayList this;
           this.size = 0;
           this.index = 0;
           this.array = (int*)malloc(sizeof(int) * /*numOfelementsToBeInserted*/);
           return this;
        }
        
        void add(struct ArrayList *list, int x) {
            list->array[list->size] = x;
            list->size++;
        }
        
        int get(struct ArrayList *list, int index) {
            return list->array[index];
         }
        

        【讨论】:

        • 我已经修改了我的代码,它与您在此处发布的类似。我改变的一件事是我将 struct 中的 array[] 变成了 int *array。我得到了预期的输出,但我也得到了“test.exe 已停止工作”的错误,通过调试,我发现这发生在你的第一行添加。知道为什么吗?
        • 如果您将 结构从 array[] 更改为 int *array,您仍然需要为其分配空间以填充元素
        • 我添加了这个:struct ArrayList *newList = malloc(sizeof(struct ArrayList) + sizeof(int) * 20);我相信这会为 int * 分配空间,对吗?
        • struct ArrayList* newList = malloc(sizeof(struct ArrayList) + sizeof(int) * 20) 它在堆中为数据类型struct ArrayList指针分配一个空间
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-16
        • 1970-01-01
        • 2014-01-31
        • 2012-02-19
        • 1970-01-01
        相关资源
        最近更新 更多