1

I wrote this function to swap values in a multi-dimensional array with my understanding that arrays are pointers.

void
swap(int* a, int* b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

However when I try to use the function

swap(board[d-1][d-2]), board[d-1][d-33];

I get these errors from the compiler and I don't know why:

fifteen.c: in function 'init':
fifteen.c:166:9: error: passing argument 1 of 'swap' makes pointer from integer without a cast [-werror]
fifteen.c:45:6: note: expected 'int *' but argument is of type 'int'
fifteen.c:166:9: error: passing argument 2 of 'swap' makes pointer from integer without a cast [-werror]
fifteen.c:45:6: note: expected 'int *' but argument is of type 'int'

How do I fix it?

5
  • 1
    Arrays are not pointers. Commented Oct 27, 2012 at 17:06
  • @chris what are they? mmm IMHO, I thought the where a big chuck of memory that you navigate through according to the data type being handles, ex.: int64_t num = 1234; and memcpy(buf, &num, sizeof(num)) in this case num behaves as an array of 8 bytes. ex.: char text[] = "..."; then int32_t *ptr=(int32_t*)&text[0]; and when you ptr++; the content of *ptr will be the next 4 bytes inside text. Commented Oct 27, 2012 at 18:36
  • @Kira, Arrays can decay into pointers, but they are not pointers themselves. Try comparing sizeofs: liveworkspace.org/code/853fc936ff59eee2994ab28ca0c02e92 Commented Oct 27, 2012 at 18:43
  • @chris, thanks for the example. It worked as you claim, but I won't lie, I'm a little skeptical about this right now. You're making me doubt XD, now I feel like I need to make an open question about this to convince myself. Thank you very much for pointing this out. Commented Oct 27, 2012 at 19:02
  • @Kira, There are some good array resources on this site and on the web. Commented Oct 27, 2012 at 19:05

3 Answers 3

3

board[d-1][d-2] and board[d-1][d-33] are int. To swap the both, you have to pass their addresses:

swap (&board[d - 1][d - 2]), &board[d - 1][d - 33];

If you are using swap (board[d - 1][d - 2]), &board[d - 1][d - 33]), the instruction int tmp = *a; will try to access to the value on the address board[d - 1][d - 2]: this make no sense! Because you are using pointers, you have to pass the address of your variables.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to pass the addresses (swap() expects two int *):

swap (&board[d-1][d-2], &board[d-1][d-33]);

Comments

0

you are passing wrong argument

swap (board[d-1][d-2]), board[d-1][d-33]);

you have to pass the address of the variable actually.So the correct way of calling this function is below.

swap (&board[d-1][d-2]), &board[d-1][d-33]);

I think this will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.