-1

I am making a rogue-like game in C and I am having trouble with file linkage. I am making a custom header file where I declare an array of structures, but when I compile this code:

#ifndef spells
#define spells

struct spells SpellList[55];

#endif // spells

I get an error: Expected identifier or '(' before '[' token.

octagonlord69
  • 75
  • 2
  • 2
  • 6

2 Answers2

3

You are using the identifier spells for two different purposes: as a "guard macro" for the header file, and as the tag-name for a struct. The compiler does not understand that you want these to be independent. With the code as shown, the preprocessing stage will replace all uses of the identifier spells with nothing, and then the parsing stage will see

struct SpellList[55];

which is invalid.

You must rename either the guard macro or the struct tag. Since you never need to refer to guard macros anywhere else, it's probably easiest to rename the guard macro.

Incidentally, "rouge" is a type of make-up. The kind of game you are making is a rogue-like.

zwol
  • 135,547
  • 38
  • 252
  • 361
1

I think the problem is using your defined symbol spells as a type.

Where you have:

struct spells SpellList[55];

The preprocessor will replace spells in that line with the value of the spells define (nothing) before the compiler tries to compile the code. This produces invalid code.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Scott Leis
  • 2,810
  • 6
  • 28
  • 42
  • 1
    When you don't give a `#define`d macro a value, it expands to _nothing_, and that's not compiler-dependent at all; it's required by the C standard. – zwol Jan 07 '18 at 23:40