Justin
Justin

Reputation: 742

How do you define a macro that accepts struct pointer arguments in C?

I'm having trouble with the following task. In particular, I have a struct:

typedef struct {
  int x;
  int y;
} foo;

I am trying to define the following operation as a macro:

#define DO_SOMETHING(a,b) ((foo){a.x + b.x, a.y + b.y})

I try to run the code below, but I'm getting an error from the compiler stating that left operand of "." must be pointer to struct/union

int main()
{
  foo a = {1,2};
  foo b = {3,4};
  foo c = DO_SOMETHING(a,b);
  return 0;
}

I've looked around for a while to try to figure out what I'm doing wrong, but I haven't been able to find a good answer. I was wondering if I could get some help with this?

Thank you.

Upvotes: 0

Views: 1664

Answers (1)

pmg
pmg

Reputation: 108938

Apparently you are using a compiler for C89/C90.

The language as defined by 1999 or later should accept your program as is.
"Compound literals" were introduced in C99.

Upvotes: 4

Related Questions