2015-11-05 87 views
0

我一直在試圖學習在C中的結構,並無法弄清楚如何使這個代碼工作。我不斷收到此錯誤:如何讓這個結構在C中工作?

incomplete type 'struct card' struct card aCard = {"Three", "Hearts"}; 

^ 
test.c:2:8: 

note: forward declaration of 
     'struct card' 
struct card aCard = {"Three", "Hearts"}; 

代碼:

#include<stdio.h> 
struct card aCard = {"Three", "Hearts"}; 

int main(void){ 
printf("%s", aCard.suit); 

} 
+11

在聲明任何變量之前,您需要定義什麼'struct card'該結構類型。就像'struct acard {const char * number; const char * suit; };' – kaylum

+0

C沒有類型推斷。 – user3386109

回答

2

首先,您需要定義結構:

struct card { 
    char type[4]; 
    int value; 
}; 

然後你就可以將它聲明:

struct card card1; /*Declaration card1 of type card*/