Passing values to macros by for loop

It is normal!
The preprocessor (the “thing” that processes the macros) is run BEFORE the C compiler. So, it is only valid when it produces compilable code.

In your case, if you use the code you show

j=Valve(1)

it will work for that value, since it will produce:

j=stTest.bValve1_Cmd

but it will do the entire loop only with that value.
When you change the parameter “1” with the “i” for actually doing the loop, then it will produce:

j=stTest.bValvei_Cmd

which is invalid.

To do what you want, just use a vector:

typedef struct OperationFlags
{
 int bValve_Cmd[2];
}FLAGS_TypeDef;
#define Valve(x) stTest.bValve_Cmd[x]
//....
for(i=1;i<=2;i++)
{
 j=Valve(1);
 printf("%d",j);
}

Leave a Comment