freeworld February 2016

Aix compilation error due to #define

"1506-221 (S) Initializer must be a valid constant expression."

In aix during compilation of header file i am facing this issue.

In the header file the source is like

#define A(b) (a+b)

like that.

wherever this macro "A"(Ex:A(5)) is used in source it throws the above error.

can anyone help me to solve this?

Edited:

in the header file

#define A(b) (a+b)

in the source file

struct a
{
   int a;
   int b;
} ain = {10, A(10)};

like this .

Answers


iharob February 2016

You can't initialize a structure with a non constant value, in this case you are using one of the structure fields I suppose but as it is your macro doesn't even know that since a isn't actually defined to be anything, try this instead

#define INIT_STRUCT(x, a, b) do {x->a = a; x->b = x->a + b} while (0);

and use it like this

struct a {int a; int b;};
INIT_STRUCT(&a, 10, 10);

NOTE: But please don't do this, instead just initialize it like this

a.a = 10;
a.b = a.a + 10;

or if you need to initialize many of these write a function instead.

Post Status

Asked in February 2016
Viewed 3,883 times
Voted 7
Answered 1 times

Search




Leave an answer