如何使这个结构在C中工作?

时间:2022-04-13 20:37:48

I've been trying to learn structs in C and can't figure out how to make this code to work. I keep getting this error:

我一直在尝试用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"}; 

The code:

代码:

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

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

}

1 个解决方案

#1


2  

First you need to define the structure:

首先,您需要定义结构:

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

And then you can declare it :

然后你可以声明它:

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

#1


2  

First you need to define the structure:

首先,您需要定义结构:

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

And then you can declare it :

然后你可以声明它:

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