2015-11-05 85 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*/