0

in my project i have three files: PlayBoard.c, Board. c, Board.h, who's relevant parts appear bellow. within PlayBoard i have theBoard which is a pointer to a variable of type struct Board. when trying to compile i constantly get a "dereferencing pointer to incomplete type". moving Board's definition to Board.h helps, but is not possible (it's a homework question), so i need another way.

PlayBoard.c:

#include "Board.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>  
...
int main()
{
BoardP theBoard =//creation of new Board;
 ...
}

Board.h:

#ifndef BOARD_H
#define BOARD_H


// ------------------------------ includes ------------------------------

#include <stdbool.h>
#include <stdio.h>

// --------------------------  definitions -------------------------

 /**
  * A pointer to Gomoku table.
 *
 */


typedef struct Board* BoardP;
/**
  * A struct of a Gomoku table.
  */
typedef const struct Board* ConstBoardP;

...

#endif

and Board.c:

#ifndef BOARD_C
#define BOARD_C

#include "Board.h"


typedef struct Board
{
    char **_board;
    int _cols;
    int _rows;
    bool _turn;
    int _xCounter;
    int _oCounter;
    int _lastx;
    int _lasty;

} Board;

...
#endif
proton
  • 393
  • 6
  • 31
  • and how do you compile? – Sourav Ghosh Mar 08 '16 at 19:38
  • 1
    Please create a [mcve] – n. m. could be an AI Mar 08 '16 at 19:40
  • `BoardP theBoard` needs a complete type. You don't have one at that point. – juanchopanza Mar 08 '16 at 19:40
  • with the compile button. but also with this make command: PlayBoard: PlayBoard.c Board.h Board.c gcc -DNDEBUG -Wextra -Wall -Wvla -std=c99 PlayBoard.c Board.h Board.c -o PlayBoard – proton Mar 08 '16 at 19:40
  • In general, compiler errors are not "false". It is much more likely that you made a mistake in your code. Which you did. – juanchopanza Mar 08 '16 at 19:41
  • Do you really need the `typedef`? –  Mar 08 '16 at 19:42
  • 1
    You don't need include guards in C source files (usually); you don't include C source files, so the protection isn't relevant. You can only initialize structures where the structure definition is known. You can do it in `Board.c`; you can't do it in `PlayBoard.c`. You write a 'create board' function in `Board.c` that returns a pointer to the `Board`, and you store that pointer in the `main()` program — and pass it around to the functions that need it. And no, it isn't a 'false error'; it is an entirely accurate error. You can only pass and return pointers to opaque (incomplete) types. – Jonathan Leffler Mar 08 '16 at 19:43
  • @JonathanLeffler simply using a pointer seems like a good solution. – proton Mar 08 '16 at 19:48

1 Answers1

2

The complete type of your struct is in Board.c, which your PlayBoard.c knows nothing about. All it has is a declaration of a struct that you have yet to define (in your Board.h file). Move the definition of the struct to within the header file. Barring this, ensure that PlayBoard.c only uses a pointer to Board (or a BoardP, as per your typedef). You do not need a complete type to declare a pointer. This ensures that any usage of the struct Board is confined to Board.c (which I assume is the point of your assignment).

R_Kapp
  • 2,818
  • 1
  • 18
  • 32
  • i cannot move the definition. how can i get PlayBoard to know Board? – proton Mar 08 '16 at 19:45
  • It is better to use pointers to the structure in all the source files except `Board.c` which actually manipulates the board structure. See also the duplicate, which goes through this explicitly. – Jonathan Leffler Mar 08 '16 at 19:52