# Thread: swapping function 2 dimensional arrays

1. ## swapping function 2 dimensional arrays

Hello,

I am working on a swap function with 2 dimensional arrays. I have it working if I write it as an If statement but cannot get it to work as a function.

It should basically initialize the array with the square root of whatever you give d and then print the array to the screen so a 4*4 array should print out as

15 14 13 12
11 10 9 8
7 6 5 4
3 2 1 0

The swap function should swap the 2 and the 1 so it should print out as
15 14 13 12
11 10 9 8
7 6 5 4
3 1 2 0

I tried to use pointers because I believe that would be best but I removed them because I could not figure out how to pass a two dimensional array that I needed to perform math on as a pointer to the function.

Attempting to use it as a function call.
Code:
```void
init(void)
{
int row,col,num=d*d,remain=d%2;
// Initalize the board with a two dimensional array.

for(row=0;row<d;row++) {
for(col=0;col<d;col++) {
num-=1; /* Start subtracting 1 from d*d Example 16-1=15 */
board[row][col] = num;
}
}
if(remain==0) {
swap(board[d-1][d-2],board[d-1][d-3]);
}
}

/*
* Prints the board in its current state.
*/

void
draw(void)
{
// Draw the board based upon the argument provided running the game.
int row,col;
for(row=0;row<d;row++) {
printf("\n");
for(col=0;col<d;col++) {
printf("%3d",board[row][col]);
}
}
printf("\n");
}

/* I know this function doesn't do anything.  I cannot figure out how to use pointers so that it changes the values of the address of the array element being passed to it. */
void
swap(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
temp = 0;
draw();
}```
Any help would be great.

Thanks,

Wayne

2. Did you try using pointers at all? It shouldn't matter that you want to swap two elements in a matrix, the actual swapping part doesn't change because of how you use it.

3. I have tried them however I only have a basic conception of pointers and was not able to get it to work. It kept telling me that I could send an integer argument without casting it as a pointer.

Excerpts from above
I tried to use pointers because I believe that would be best but I removed them because I could not figure out how to pass a two dimensional array that I needed to perform math on as a pointer to the function.

/* I know this function doesn't do anything. I cannot figure out how to use pointers so that it changes the values of the address of the array element being passed to it. */

4. Well a proper swap needs pointers as arguments so put the pointers back. You cannot safely swap two things by value (although it is possible, it is also pretty dumb).
Then you rewrite swap to swap two things using pointers.
Then:
Code:
`swap(&board[d-1][d-2], &board[d-1][d-3]);`
This, because the value of a pointer is a memory address, as you should recall from your studying. Before, you were only passing in the values of the elements you wanted to swap.

5. It gives me the errors when I just do that. I have tried the following:

gcc -ggdb -std=c99 -Wall -Werror -o fifteen fifteen.c -lcs50 -lm
fifteen.c: In function 'init':
fifteen.c:152:9: error: passing argument 1 of 'swap' makes integer from pointer without a cast [-Werror]
fifteen.c:45:6: note: expected 'int' but argument is of type 'int *'
fifteen.c:152:9: error: passing argument 2 of 'swap' makes integer from pointer without a cast [-Werror]
fifteen.c:45:6: note: expected 'int' but argument is of type 'int *'
fifteen.c: At top level:
fifteen.c:200:1: error: conflicting types for 'swap'
fifteen.c:45:6: note: previous declaration of 'swap' was here
cc1: all warnings being treated as errors

Code:
```void
init(void)
{
int row,col,num=d*d,remain=d%2;
// Initalize the board with a two dimensional array.

for(row=0;row<d;row++) {
for(col=0;col<d;col++) {
num-=1; /* Start subtracting 1 from d*d Example 16-1=15 */
board[row][col] = num;
}
}
if(remain==0) {
swap(&board[d-1][d-2],&board[d-1][d-3]);
}
}

void
swap(int* pa, int* pb) {
int* ptemp;
ptemp = pa;
pa = pb;
pb = ptemp;
//ptemp = 0;
draw();
}```

6. I'm more concerned with top-level errors because the code is largely correct.
fifteen.c:200:1: error: conflicting types for 'swap'
fifteen.c:45:6: note: previous declaration of 'swap' was here
You only need one declaration for swap, that makes it a function that accepts two pointers. You probably forgot to update the swap function prototype when you changed how it works.

Also I don't think your swap is correct. Normally you have to dereference both pointers repeatedly to make a swap.

7. You are exactly correct on two counts. I did forget to update the prototype. I had never seen that error before. My swap also did not work. I don't know what you mean by dereference. Any possibility of an example? Thank you for the help by the way.

8. Originally Posted by scrfix
My swap also did not work. I don't know what you mean by dereference. Any possibility of an example? Thank you for the help by the way.
Code:
```void swap(int *pa, int *pb)
{
int hold = *pa; /* the operator * dereferences pointers */
*pa = *pb;
*pb = hold;
}```
Like that, note the differences in my code. The object being pointed to is retrieved when you dereference a pointer.

You are welcome.