This definition:
#define COPY(x,y,z) \
a->x = x;\
a->y = y;\
a->z = z;\
With a for loop
for(i=0;i<n;i++)
COPY(b->x,b->y,b->z);
for(i=0;i<n;i++)
COPY(c->x,c->y,c->z);
Which results in this quick test:
#include <stdio.h>
#define COPY(x,y,z) \
a->x = x;\
a->y = y;\
a->z = z;\
int main( int argc, char* argv[] )
{
for(i=0;i<n;i++)
COPY(b->x,b->y,b->z);
for(i=0;i<n;i++)
COPY(c->x,c->y,c->z);
}
Only pre-processed with: gcc -E main.c > main.i
Gives
int main( int argc, char* argv[] )
{
int i;
for(i=0;i<n;i++)
a->b->x = b->x; a->b->y = b->y; a->b->z = b->z;;
for(i=0;i<n;i++)
a->c->x = c->x; a->c->y = c->y; a->c->z = c->z;;
}
There's a lot wrong!
You have two places where macro replacement's ocurring in a->x = x
because both x's get replaced, so firstly change to unique identifiers. Secondly, do not encode the copy-to element in the define. In your case this is a, make this something passed to the macro:
#define COPY( dest, x_copy, y_copy, z_copy ) \
dest->x = x_copy; \
dest->y = y_copy; \
dest->z = z_copy;
You've also fallen into the trap of having if followed by a multiple-statement macro which will obviously not perform how you intended because you've not surrounded the multi-line macro with curly braces! It's common practice to wrap multiple line macros in do{ } while(0)
so that they can be used as you would expect:
#define COPY( dest, x_copy, y_copy, z_copy ) \
do { \
dest->x = x_copy; \
dest->y = y_copy; \
dest->z = z_copy; \
} while(0)
Now, replacing your macro which this new one results in an output we'd expect to work:
#include <stdio.h>
#define COPY( dest, x_copy, y_copy, z_copy ) \
do { \
dest->x = x_copy; \
dest->y = y_copy; \
dest->z = z_copy; \
} while(0)
int main( int argc, char* argv[] )
{
int i;
for(i=0;i<n;i++)
COPY(a, b->x,b->y,b->z);
for(i=0;i<n;i++)
COPY(a, c->x,c->y,c->z);
}
Again, gcc -E main.c > main.i
results in the correct code expanded from the macro:
int main( int argc, char* argv[] )
{
int i;
for(i=0;i<n;i++)
do { a->x = b->x; a->y = b->y; a->z = b->z; } while(0);
for(i=0;i<n;i++)
do { a->x = c->x; a->y = c->y; a->z = c->z; } while(0);
}