C语言 '&' 标记前的预期声明说明符或 '...'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35952154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:21:58  来源:igfitidea点击:

Expected Declaration Specifiers or ‘...’ before ‘&’ token

ccompiler-errors

提问by DotERM

I'm a beginner in C, i just have 13 years, so i'm pretty sure that the error is something very basic.

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

I was bored in school, i started writing C about a "Game".

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

Here is it:

就这个:

#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
main.c:11:15: error: expected declaration specifiers or ‘...' before ‘&' token
 int allocData(&entity) {
               ^
"; printf("ID: %d, Name: %s", player.id, player.name); return 0; }

But GCC complains.

但海湾合作委员会抱怨。

int allocData(entity *player);

I can't find the error, and i didn't want to post here, i'm sure is something stupid that i missed.

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

回答by dinomario10

Your function definition should take pointer as an argument:

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

int allocData(entity * p_entity)

回答by artm

Your prototype looks wrong:

你的原型看起来不对:

int allocData(&entity) // <-- what is the type? and why it's address operator here??

int allocData(&entity) // <-- 类型是什么?为什么它是地址运算符?

It should be:

它应该是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* add this for using strcpy() */

typedef struct {
    int id;
    int posx;
    int posy;
    char name[129];
} entity;

/* now allocData() won't be needed because entity doesn't have any member to allocate pointer to */

int main(void) {
    entity player;
    player.id = 0;
    /* use strcpy() to copy strings */
    /* ##代码## here will be meaningless in this case, so I removed this */
    strcpy(player.name, "loopback");
    printf("ID: %d, Name: %s", player.id, player.name);
    return 0;
}

You should also replace all your entityvariable name in function allocData- remember entityis your typename, not variable.

您还应该替换entity函数中的所有变量名称allocData- 记住entity是您的类型名称,而不是变量。

回答by MikeCAT

You cannot use reference in C.

你不能在 C 中使用引用。

In this case, you should want to use a pointer.

在这种情况下,您应该想要使用指针。

change the lineint allocData(&entity) {to int allocData(entity *entity) {, and the code will compile.

将行更改int allocData(&entity) {int allocData(entity *entity) {,代码将编译。

Although this change will make the code compile, you will invoke undefined behaviorby passing data having wrong type to printf(): %dexpects for int, but you passed int*. You will also cause memory leak by throwing away the pointer assinged in allocData(). I don't think using pointer unless they are needed isn't good.

尽管此更改将使代码编译,但您将通过将具有错误类型的数据传递给: expects for来调用未定义的行为,但您传递了. 您还将通过丢弃 .assinged 中的指针导致内存泄漏。我不认为使用指针除非需要它们不好。printf()%dintint*allocData()

Your code should be like this:

你的代码应该是这样的:

##代码##