繁体   English   中英

结构未初始化

[英]Struct not initializing

试图初始化四个结构,但它说未定义。 该程序在 c 中,使用 gcc 作为编译器。

代码如下:

struct Deck_init{
    int card1, card2;
};

// Initialize player decks
//Deck_init player1_hand, player2_hand, player3_hand, player4_hand; // Need this to work
//Deck_init player1_hand {0,0}; // Test line
//Deck_init player1_hand; // Test line

错误:

identifier "Deck_init" is undefined

如果需要,这里是到目前为止的代码:

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#define NUM_THREADS 4 // Number of players 
#define NUM_CARDS_IN_DECK 52 // Cards in deck
#define PTHREADM PTHREAD_MUTEX_INITIALIZER
#define PTHREADC PTHREAD_COND_INITIALIZER


struct Deck_init{
    int card1, card2;
};

// Initialize player decks
Deck_init player1_hand, player2_hand, player3_hand, player4_hand; // Need this to work
//Deck_init player1_hand {0,0}; // Test line
//Deck_init player1_hand; // Test line

我做了什么:

  • 尝试初始化一个 object
  • 尝试将问题标记到它自己的单独文件中,但仍然存在问题。

在 C 中,声明变量时必须包含struct关键字:

struct Deck_Init player_hand1, player_hand2; // .. etc

或者,您可以使用typedef创建具有不同名称的struct Deck_Init的别名。 简单地“删除” struct部分是很常见的,但是您可以将typedef您喜欢的任何语法上有效的名称:

typedef struct Deck_Init{
    int card1, card;
} Deck_Init; // could just as easily be MyCoolNewDeck

...

// now you can omit the struct part
Deck_Init player_hand1; // etc..

例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM