0

I’m looking for help here. I know that the topic sound familiar, but I’ve looked in existing topic but couldn’t find a solution to my problem. I just beginning with C language and try to make this simple tic tac toe game with using of malloc but before I can even start to make mistakes I get error at the beginning. So there is mine main.c, game.h and game.c

main.c

    #include <stdio.h>
    #include <stdlib.h>
    #include "game.h"

   int main(int argc, char* argv[])
   {
   play_game();
   return 0;
   }

game.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "game.h"

   void play_game()
   {
    printf("Xs and Os!\n");
    struct game *p_game_info = 0;
    p_game_info = (struct game*)malloc(sizeof(struct game));
    initialise_game (p_game_info, "John", "Annie");
    draw_banner();
    display_board(p_game_info->board);


   }
  void initialise_game(struct game* p_game_info, char* name1, char* name2)
   {
    p_game_info->status = P1_TURN;

    for (int r=0; r<3; r++)
        for(int c=0; c<3; c++)
        p_game_info->board[r][c] = SPACE;

    strncpy(p_game_info->playerNames[0], name1,MAX_NAME_LEN);
    strncpy(p_game_info->playerNames[1], name2,MAX_NAME_LEN);
   }
 void draw_banner(){

   printf("---------------\n");
   printf("  GAME STATUS  \n");
   printf("---------------\n");
   }
    void display_board(  char board[3][3]){
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < 3; j++)
    {
        printf("%c", board[i][j]);
        if (j != 2)
            printf("|");
    }
    if (i != 2)
        printf("\n---------");
    printf("\n");
   }
 }

game.h

   #ifndef GAME_H_INCLUDED
   #define GAME_H_INCLUDED
   #define MAX_NAME_LEN 50
   enum Bool { False, True };
   enum status { P1_TURN, P2_TURN, P1_WON, P2_WON, DRAW };
   typedef enum Bool boolean;
   const char SPACE= '-';
   const char X_SYMBOL = 'X';
   const char O_SYMBOL = 'O';

   struct game {
   char board[3][3];
    char playerNames[2][MAX_NAME_LEN];
   int status;
   boolean finished;
   };
  #endif 

errors in function main.c multiple definition of 'X_SYMBOL' first defined here (points to void play_game(){) multiple definition of 'Y_SYMBOL' first defined here (points to void play_game(){) error:ld returned 1 exit status

Could anyone of you help me with that so I can go further and check if my code has any sense at all thanks

  • 3
    Either define the variables in the header file as `static`, or as `extern`. – Some programmer dude Apr 29 '18 at 16:39
  • In C `const char X_SYMBOL = 'X';` creates a global variable, and because you include the header in more source files it is multiply defined. While this technique can work in some C++, it doesn't in pure C. Replace it with a `#define` as `#define X_SYMBOL 'X';`, which is more appropriate in plain C. – Frankie_C Apr 29 '18 at 16:45
  • hahaha, thanks Some programmer dude! it works :) that's great oneliner answer :) – Maciej Kubiniec Apr 29 '18 at 16:45
  • Just for info, using the storage class `static` will create duplicate symbols with file scope. It works, but isn't strictly correct because means to fill memory with duplicates at each header inclusion. On the other hand using the storage specifier `extern` will expect a global declaration in at least one source, but in your code there is none. Some linkers can accept it creating an instance on the fly, but this is not **ISO/IEC** compliant and is definitely a programming error. – Frankie_C Apr 29 '18 at 17:12

0 Answers0