2015-02-11 77 views
-1

我遇到了结构问题。在每个函数声明之前,我会收到有关标识符的错误。 '类型定义', 'COORDS stackCreate' 之前的错误发生, 'COORDS stackPush'预期标识符 - C

typedef struct coords * coordPtr 
{ 
    int x = -1; 
    int y = -1; 
    struct coords * next; 
}; 

coords stackCreate(int x, int y){ 
    coordPtr stack = malloc(sizeof(coords)); 
    stack->x = x; 
    stack->y = y; 
    return stack; 
} 

coords stackPush(int x, int y, coords stack){ 
stack->next = malloc(sizeof(coords)); 
stack->next->x = x; 
stack->next->y = y; 
} 

感谢您的帮助!

+1

好了,你有'typedef结构COORDS * coordPtr' ---这绝对不是正确的C. – 2015-02-11 20:35:30

+0

你尝试过: typedef结构_coords { int x = -1; int y = -1; struct _coords * next; } coords; – madz 2015-02-11 20:36:53

+4

这是对C语法的一个简单误解:'* coordPtr'在结构体之后,而不是在它之前。投票结束为错字。 – dasblinkenlight 2015-02-11 20:36:58

回答

5
typedef struct coords * coordPtr 
{ 
    int x = -1; 
    int y = -1; 
    struct coords * next; 
}; 

应该

typedef struct coords 
{ 
    int x; 
    int y; 
    struct coords * next; 
} *coordPtr; 

类型的别名来最后。你也不能在结构声明中提供默认的初始值设定项。

编辑:在你的程序

此外,您还利用两个类型别名:coordscoordPtr。如果你也想用coords,您还需要:

typedef struct coords coords; 
+0

我切换它,并删除了默认初始值设定项,并且仍然收到相同的错误 – 2015-02-11 20:44:13

+0

@IanPennebaker看到我的编辑 – ouah 2015-02-11 20:49:42