c - Macro SWAP(t,x,y) exchanging two arguments of type t -


so trying make swap(t,x,y) macro exchanges 2 arguments of type t. trying think of going around problem when these 2 arguments of form

v[i++] , w[f(x)] , i.e. swap(int, v[i++], w[f(x)]).

the code below crashing ...

#define swap(t,x,y) {t *p = x; t *q = y; t z = *p; *p = *q; *q = z;}  int f (int x){     return (0-x); }  int main(void) {  int v[] = {1,2,3}; int = 0;  int w[] = {4,5,6}; int x = -1;  int *p = v; int *q = w;  swap(int*, v[i++],w[f(x)]);  return 0; } 

any ideas may go wrong?

swap(int*, v[i++],w[f(x)]); 

v[i++] int element assigning pointer object in:

t *p = x; 

so when dereferencing p in t z = *p; segfault. if want pointer element use & operator.

moreover v[i++] has side-effect (it modifies i++) , should never pass expressions have side-effects in macro call.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -