Hy! this is my first post on stack overflow, I know it's pretty long, hope I won't get banned!

Probably one of the most elegant approaches would be using a function that swaps the two received arguments - using it to swap matrix components. Let's say somethig like swap(a,b). As many have already said, we should consider using a auxiliary variable

```auxiliary = a ;
a = b ;
b = auxiliary ;```

Recently, I picked up a new method, which I found impressing, using bitwise XOR operation (http://en.wikipedia.org/wiki/Xor) thus a auxiliary is not needed

```a ^= b ;
b ^= a ;
a ^= b ;```

You can easily use this operation to swap two elements ( a and b ) - I belive this is off topic, but I insisted on this idea because I found it pretty interesting. Finally, answering your question, you could use let's say

```int swap (int *a , int *b){
(*a)^=(*b);
(*b)^=(*a);
(*a)^=(*b);
return 0;
}```

while having a matrix declared as

```#define ROW_COUNT 5
#define COLUMN_COUNT 5
....
int a[ROW_COUNT][COLUMN_COUNT];```

You can use your the XOR way the swap the rows, firstly identifyng the elements needed to be swapped ( according to row index, as you already said )

```printf("\nSwap Row: "); scanf("%d", &swp1) ; // first row index
printf("With Row: "); scanf("%d", &swp2);    // second row index

for (j = 0 ; j < COLUMN_COUNT ; j++){
swap(  &a[swp1][j] , &a[swp2][j] );
}```

Also try this example, I'm sure you'll understand the whole idea much better afterwards (don't forget matrix index starts at 0 !)

```#include "stdio.h"
#include "conio.h"

#define ROW_COUNT 5
#define COLUMN_COUNT 5

int swap (int *a , int *b){
(*a)^=(*b);
(*b)^=(*a);
(*a)^=(*b);
return 0;

}

int main(){
int i, j ;
int swp1, swp2 ;
int a[ROW_COUNT][COLUMN_COUNT];

// Create ( ROW_COUNT X COLUMN_COUNT ) random matrix

for (i = 0 ; i < ROW_COUNT ; i++ )
for (j = 0 ; j < COLUMN_COUNT ; j++ )  a[i][j] = rand();

// Display matrix before row swap

for (i = 0 ; i < ROW_COUNT ; i++ ){
for (j = 0 ; j < COLUMN_COUNT ; j++ )  printf("%d\t",a[i][j]);
printf("\n");
}

// Elements to be swapped

printf("\nSwap Row: "); scanf("%d", &swp1) ;  // first row index
printf("With Row: "); scanf("%d", &swp2);     // second row index

// Swapping right here

for (j = 0 ; j < COLUMN_COUNT ; j++){
swap(  &a[swp1][j] , &a[swp2][j] );
}

// Display once again

printf("\n");
for (i = 0 ; i < ROW_COUNT ; i++ ){
for (j = 0 ; j < COLUMN_COUNT ; j++ )  printf("%d\t",a[i][j]);
printf("\n");
}

getch();
return 0;
}```

There is rarely anything to be gained by using the XOR swap (it generally isn't any faster and is less clear), and the implementation you give above has a horrible failure mode if a and b are the same pointer.

## how do you swap two rows in a matrix (in C)? - Stack Overflow

c matrix row swap