CS Student
CS Student

Reputation: 1643

Extern, headers, global variables

I'm making a game, the current structure of my program is like the following...

prog structure

The problem I'm having is that I have a function in init_game.c that initialises the players boards with the necessary data, once I have these initialised boards I need the_game.c to use these initialised boards (from init_game.c).

I'm confused on how I would get this to work with extern's

EDIT: Clarification - I'm wondering how I can use the extern keyword with my programs structure to allow me to use the players initialised 2D char array (which gets initialised in the file init_game.c but is first defined in the attributes.h file) in the file the_game.c

Added a picture below for clarification to my problem... enter image description here

Upvotes: 1

Views: 170

Answers (2)

Lundin
Lundin

Reputation: 215114

This is a clear indication that your program design is flawed. The correct solution is then to fix the program design, not to implement spaghetti coding with global variables.

The best way to use this is to use object-oriented design, by creating "classes" or "ADTs" or "code modules" (these things basically mean the same thing in C, as there is no class keyword). Each such class consists of a .h file and a .c file. Use the object-oriented concepts of keeping classes autonomous, only doing their specific task without caring about the rest of the world. Use private encapsulation and put everything that is to be regarded as public in the .h file.

  • Game is a class in your project. Therefore, everything that is related to the game shall be in that class.
  • Initializing the game is most definitely related to the game class, so that code should be merged with game.h. It doesn't make sense to have it in a file of its own.
  • If either of those files contain items that don't belong to the game class, such items needs to be moved to files of their own.
  • If the game class proves to be too complex, which it most likely will, you need to split up the various properties of the game into sub-classes. For example, one class for handling boards, one class for handling players and so on.
  • If your program design is correct, there should not be any global variables anywhere. If you ever find yourself using the extern keyword, your program design is flawed. Please note that static file scope variables are perfectly fine to use (though consider thread-safety issues).

Upvotes: 1

downhillFromHere
downhillFromHere

Reputation: 1977

In init_game.c, declare global variables,

char player_board[BOARD_SIZE][BOARD_SIZE];
char enemy_board[BOARD_SIZE][BOARD_SIZE];

Any file that includes attributes.h will then have access to these arrays, provided that file doesn't declare another variable with the same name.

In your main, you need to make sure that you call the initializing function from init_game.c before you call any function from the_game.c that uses the global arrays.

Upvotes: 0

Related Questions