Structs in C

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

My current assignment in my C programming class is to create a text based battleship game. Right now I'm using structs for a few sets of data, one of which is my players.

Heres enum and structs

Code: [ Select ]
/* ENUM TYPES */
typedef enum boolean
{
    FALSE, TRUE
} Boolean;

typedef enum ship
{
    EMPTY, CARRIER, BATTLESHIP, CRUISER, SUBMARINE, DESTROYER
} Ship;

/* STRUCTS */
typedef struct cell
{
    Ship ship;         // Stores what ships is in this cell
    Boolean hit;      // Stores where or not the cell has been shot at.
    Boolean occupied; // Stores if there is a ship in this cell
    int propability;
} Cell;

typedef struct player
{
    int id;                                     // Players id
    Cell game_board[NUM_ROWS][NUM_COLS];      // This board shows the players battleship positions
    Cell logic_board[NUM_ROWS][NUM_COLS];      // This board shows the logic for the player or computer
                                                // Will hold hits, misses for players and propability for
                                                // the computer.
} Player;
  1. /* ENUM TYPES */
  2. typedef enum boolean
  3. {
  4.     FALSE, TRUE
  5. } Boolean;
  6. typedef enum ship
  7. {
  8.     EMPTY, CARRIER, BATTLESHIP, CRUISER, SUBMARINE, DESTROYER
  9. } Ship;
  10. /* STRUCTS */
  11. typedef struct cell
  12. {
  13.     Ship ship;         // Stores what ships is in this cell
  14.     Boolean hit;      // Stores where or not the cell has been shot at.
  15.     Boolean occupied; // Stores if there is a ship in this cell
  16.     int propability;
  17. } Cell;
  18. typedef struct player
  19. {
  20.     int id;                                     // Players id
  21.     Cell game_board[NUM_ROWS][NUM_COLS];      // This board shows the players battleship positions
  22.     Cell logic_board[NUM_ROWS][NUM_COLS];      // This board shows the logic for the player or computer
  23.                                                 // Will hold hits, misses for players and propability for
  24.                                                 // the computer.
  25. } Player;


Now in my main program I have something like this:

Code: [ Select ]
int main (void)
{

/****************************************
    *             INITIALIZE              *
    ****************************************/
    
    FILE *log_file = NULL;         // Log file handler
    int current_players_turn = 0;
    Player player0;      // Create players
    Player player1;      // Create players

    log_file = fopen(LOG_FILE,"w"); // Open file for loging

    initialize_player(player0,0); // Human Player
    initialize_player(player1,1); // Computer Player
    
    current_players_turn = select_who_starts_first();     // Randomly choose first player
    
    welcome_screen();              // Displays welcome screen
.........
  1. int main (void)
  2. {
  3. /****************************************
  4.     *             INITIALIZE              *
  5.     ****************************************/
  6.     
  7.     FILE *log_file = NULL;         // Log file handler
  8.     int current_players_turn = 0;
  9.     Player player0;      // Create players
  10.     Player player1;      // Create players
  11.     log_file = fopen(LOG_FILE,"w"); // Open file for loging
  12.     initialize_player(player0,0); // Human Player
  13.     initialize_player(player1,1); // Computer Player
  14.     
  15.     current_players_turn = select_who_starts_first();     // Randomly choose first player
  16.     
  17.     welcome_screen();              // Displays welcome screen
  18. .........


When I run this I get a run-time check failure saying that
Quote:
Run-Time Check Failure #3 - The variable 'player0' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'player1' is being used without being initialized.


Any idea why this is happening? My teacher hasn't really talked to us about how to "initialize" a struct, all we have learned to do is create an instance of one.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I'll after a while of searching the web and talking to some friends I found that I just needed to pass the memeory address of the struct varible to the function initialize_player();
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

Cool, I did that exact same project in a computer science course during my freshman year, only we did it in Java.

The error you're getting is a Microsoft-specific runtime error. Chances are (not 100%, I haven't tried) that if you were to compile with GCC and run your code, it would work just fine.

Basically, when you declare a variable on the stack without immediately initializing it, the contents of the memory referenced by that variable will contain garbage -- whatever data was in that block of memory before you acquired it. Look at the following example:

C Code: [ Select ]
int myFirstInteger;
int mySecondInteger = 5;
  1. int myFirstInteger;
  2. int mySecondInteger = 5;


In the code above, myFirstInteger is declared but not initialized. The value of that variable is undefined -- it could be any valid integer value, and using it at that point would produce undefined results.

The mySecondInteger variable, on the other hand, has been declared and initialized, and accessing it is just fine. GCC doesn't baby you like Microsoft does, it just assumes that you know what you're doing.

Now, the same deal applies to structs. When you declare a struct variable the way you have in your code, it allocates a block of memory on the stack to represent the entire contents of the struct. However, this block is filled with garbage just like any other variable. You need to go through and initialize all the data -- members of the struct -- including any members of structs contained within that struct -- before you can go accessing the memory.

I'm sure that your initialize_player() method was intended to do exactly that, but you probably missed some values somewhere in your initialization that's causing Microsoft to cry.

Go through your initialization routine and make sure that you're initializing every member variable of your Player struct with a value. If any of those members is a struct itself, make sure to initialize its members with values as well.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

SpooF wrote:
I'll after a while of searching the web and talking to some friends I found that I just needed to pass the memeory address of the struct varible to the function initialize_player();

Yes, or that. :lol:

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 100 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.