0

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.
3
  • 3
    For C, you need to do struct Deck_init or typedef the struct part away. Commented Nov 14, 2022 at 23:00
  • 1
    Or if you are serious about this being C++, this code does compile as C++. Perhaps you are confusing the two languages? Commented Nov 14, 2022 at 23:01
  • 1
    Sorry, I didn't mean to add the C++ tag, mistakenly did that. Thanks for removing it! It's definetly meant to be in C Commented Nov 14, 2022 at 23:06

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

This was it! Thank you so much! I'll put this as solution once it allows me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.