I'm using the following swap function to swap two elements of an array. The element pointed to by 'x' and 'y' and the one right below it, to be particular (then i want x and y to point to the second of the swapped elements, hence i use auto increment). I use the following call in the main function to call the swap function:

swap(&a[x][y], &a[++x][y]);

And the swap function looks like this.

void swap(int* p, int* q)
 int temp;
  temp = *p;
 *p = *q;
 *q = temp;

I dunno where i've gone wrong- is it the improper usage of pointers or auto increment operator or both? I feel it's the code on line 5 that's the culprit.

Any suggestions?

8 Years
Discussion Span
Last Post by anveshnaidu

> swap(&a[x][y], &a[++x][y]); pre-incrementing here is undefined behavior. Explanation can be found here.

Votes + Comments
Indeed it is so
Solved the prob in a jiffy :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.