Home > Back-end >  Struct not initializing
Struct not initializing

Time:11-15

Trying to initialize four structs but it says undefined. The program is in c and using gcc as compiler.

Code below:

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

Error:

identifier "Deck_init" is undefined

If needed, here's the code up to that point:

#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

What I've done:

  • Tried initializing one object
  • Tried singaling the problem into it's own seperate file and still problems.

CodePudding user response:

In C, you have to include the struct keyword when declaring a variable:

struct Deck_Init player_hand1, player_hand2; // .. etc

or, you can use typedef to create an alias of struct Deck_Init with a different name. It's common to simply "remove" the struct part, but you can typedef to any syntactically-valid name you like:

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..

Example

  • Related