Ursa Major
Ursa Major

Reputation: 901

Syntax error before string constant

I am seeing "syntax error before string constant at line ' testFunction(45, UP),'"

#define UP   "UP\0"
#define DOWN "DOWN\0"

#define testFunction(intensity, direction) \
    { \
      .force        = intensity, \
      .direction    = direction, \
    }

struct configureObject {
  int   force;
  char direction[7];

};

static const struct configureObject configureFiles[] =
{
  testFunction(45, UP),
  testFunction(46, DOWN),
};

in main()

    printf("force: %d\n", configureFiles[0].force);
    printf("direction: %s\n", configureFiles[0].direction);        

    printf("force: %d\n", configureFiles[1].force);
    printf("direction: %s\n", configureFiles[1].direction); 

There are no other compiler hints. What may be the reason for this error? Thank you.

Upvotes: 1

Views: 2741

Answers (1)

NPE
NPE

Reputation: 500773

The problem is that you use direction for two different things in:

.direction    = direction,

Both get substituted.

Try:

#define testFunction(intensity, dir) \
    { \
      .force        = intensity, \
      .direction    = dir, \
    }

(This is just an illustration, there's probably a better name than dir.)

Upvotes: 2

Related Questions