c - ‘...’ token 之前的预期声明说明符或 ‘&’

标签 c compiler-errors

我是 C 的初学者,我只有 13 年,所以我很确定这个错误是非常基本的。

我在学校很无聊,我开始写关于“游戏”的C。

这是它:

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

typedef struct {
    int * id;
    int * posx;
    int * posy;
    char * name;
} entity;

int allocData(&entity) {
    entity->id = malloc(sizeof(int));
    if(entity->id == NULL) {
        return 1;
    }
    entity->posx = malloc(sizeof(int));
    if(entity->posx == NULL) {
        return ;
    }
    entity->posy = malloc(sizeof(int));
    if(entity->posy = NULL) {
        return 1;
    }
    entity->name = malloc(sizeof(char) * 129);
    if(entity->name == NULL) {
        return 1;
    }
    return 0;
}

int main() {
    entity player;
    allocData(&player);
    player.id = 0;
    player.name = "loopback\0";
    printf("ID: %d, Name: %s", player.id, player.name);
    return 0;
}

但是 GCC 提示。

main.c:11:15: error: expected declaration specifiers or ‘...’ before ‘&’ token
 int allocData(&entity) {
               ^

我找不到错误,我不想在这里发帖,我确定我错过了一些愚蠢的事情。

最佳答案

您的函数定义应将指针作为参数:

int allocData(entity *player);

关于c - ‘...’ token 之前的预期声明说明符或 ‘&’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35952154/

相关文章:

java - “找不到符号”或“无法解析符号”错误是什么意思?

c++ - 在类之间传递多维数组

c - 如何从特定函数返回数组?

c - 如何在 C 中初始化空结构类型

c - 如何在 c 套接字中使用 select 等待特定响应而不忽略其他响应?

java - 为什么Java中Integer对象的计数速度比int慢

c - 仅当参数不是常量时,math.h 中的 sqrt 才会导致链接器错误 "undefined reference to sqrt"

c - 从 Windows 内部修改 SMBIOS

java - 使用 JNI 将指向 double 组的 C 指针传递给 Java

compiler-errors - 显示为无法识别的类型类