2015-02-11 76 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