0

I'm working on a school project right now and i need to define two structs as addresses, like the code below shows :

typedef struct board_t* board;
/**
 * @brief Pointer to the structure that holds the game.
 */

typedef struct piece_t* piece;
/**
 * @brief Pointer to the structure that holds a piece
 */

and if i let it like it, it compiles. However, as soon as I try replacing the semicolon by a bracket to define the struct, I get a compilation error. Here's the code and the error :

typedef struct piece_t* piece{
/**
 * @brief Pointer to the structure that holds a piece
 */
 enum shape p_shape;
 enum size p_size;
 enum color p_color;
 enum top p_top;
 enum players author;
};


typedef struct board_t* board{
/**
 * @brief Pointer to the structure that holds the game.
 */
 piece array[4][4];
}

And the error :

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
   53 | typedef struct board_t* board{

What i need to do is create a board filled with pieces that i can edit inside functions. Can anyone help me?

Frexom
  • 53
  • 7
  • 1
    Just avoid the typedef completely. Using a typedef to obscure the fact that a type is a pointer is a recipe for ongoing maintenance troubles. There are certain occasions when typedefs are useful, but they should be used judiciously. – William Pursell Jan 01 '22 at 13:38
  • @Exampleperson You can make opaque structs without using a typedef. – William Pursell Jan 01 '22 at 13:43
  • IMO, the only reasonable place for a typedef is for function pointers. – William Pursell Jan 01 '22 at 13:43
  • typedef is nice for structs so that you don't need to have "struct" as part of the typename, https://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c?rq=1 I probably wouldn't use them to hide the pointer nature of the new typename though. But at the end of the day, the compiler sets the rules, everything else is just a guideline and we need to use our judgement depending on the needs of the project. – Dikran Marsupial Jan 01 '22 at 13:47
  • i would have like to do otherwise, but the teachers asked us to do it that way, too bad :( – Frexom Jan 01 '22 at 13:52
  • @Frexom being able to conform to programming styles that we personally don't like is a useful skill for a programmer, if nothing else it can be useful to see and understand both sides of the argument. I'd put a prefix into the typename, or some other convention, just to make sure it is obvious to the reader that it is a pointer. – Dikran Marsupial Jan 01 '22 at 13:55
  • Was this line `typedef struct board_t* board;` handed to you with a requirement to use it as is? **If so**, leave it alone and do not replace anything in it. You will need to define `struct board_t { ... };` elsewhere (note no `typedef` and no `*`). **If not**, just don't use `typedef` to hide pointers. – n. m. could be an AI Jan 01 '22 at 14:37
  • @n-1-8e9-wheres-my-share-m yep, i figured out thanks to Dikran Marsupial who answered me on this post :) – Frexom Jan 01 '22 at 15:39

1 Answers1

1

I think typedef names need to be at the end

typedef struct piece_struct {
/**
 * @brief Pointer to the structure that holds a piece
 */
 enum shape p_shape;
 enum size p_size;
 enum color p_color;
 enum top p_top;
 enum players author;
}
piece;


typedef struct board_struct {
/**
 * @brief Pointer to the structure that holds the game.
 */
 piece array[4][4];
}
board;

and if you want a typedef name for the pointer, you would need to create those separately.

typedef piece* piece_ptr;
typedef board* board_ptr;

Perhaps the code is clearer if the structure definitions are separated from the typedefs:

struct piece_struct {
/**
 * @brief structure that holds a piece
 */
 enum shape p_shape;
 enum size p_size;
 enum color p_color;
 enum top p_top;
 enum players author;
};

typedef piece_str* piece;  // piece is a new name for a pointer
                           // to a piece_str

struct board_struct {
/**
 * @brief structure that holds the game.
 */
 piece array[4][4];
};

typedef struct board_struct* board;   // board is a new name for a pointer
                                      // to a board_str

Personally I tend not to make typedefs for pointers as I find it difficult to remember whether it is a pointer or the struct itself, so I make a typedef for the struct and use * when declaring a pointer.

  • So i need to create a struct with only a pointer inside pointing to a struct with the actual board? – Frexom Jan 01 '22 at 13:22
  • 2
    typedef is just creating a new name for an existing typename, so typedef piece* piece_ptr; is not creating a struct, it is just introducing piece_ptr as a new typename that is the same as piece* . It is a common idiom to wrap a struct definition inside a typedef statement, but the code may be easier to understand if you do them separately. – Dikran Marsupial Jan 01 '22 at 13:25
  • 1
    Oh i see, thanks for that answer it really will help me i hope – Frexom Jan 01 '22 at 13:35
  • no problem, hope the project goes well! – Dikran Marsupial Jan 01 '22 at 13:36
  • 1
    You can actually define multiple types in a single typedef statement: `typedef struct board { ... } board, *board_ptr;` Yet it should be avoided for readability's sake. Hiding pointers behind typedefs is also confusing and error prone. Teaching newbies such things is not helping them understand the concept of pointers. – chqrlie Jan 01 '22 at 15:22