Reputation: 29
I understand that when you pass by reference through a function in C, the parameters of the function take in the address of the pointer that will be modified. I am extremley boggled on why this example of pass by reference is not working. Can anyone point me in the right direction....
This should output a swap but when i compile the swap does not occur why is this pass by reference not working?
#include <stdio.h>
void swapnum(int *i, int *j) {
int temp = i;
i = j;
j = temp;
}
int main(void) {
int a = 10;
int b = 20;
swapnum(&a, &b);
printf("A is %d and B is %d\n", a, b);
getchar();
getchar();
return 0;
}
Upvotes: 2
Views: 870
Reputation: 113
Your implementation of swapnum() is a bit improper. The function takes 2 (int *) parameters. That is, i and j are integer pointers storing references of a and b. when you do int temp = i; you are actually assigning a pointer to integer variable (which is incorrect) and then instead of swapping the values, the code snippet plays around with addresses. This is what you need
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
Upvotes: 0
Reputation: 24167
You forgot to dereference your pointers inside the function. Thus you end up reassigning the local pointer values rather than changing the actual value being pointed to and it has no effect.
So, use the *
dereference operator:
int temp = *i;
*i = *j;
*j = temp;
Upvotes: 6
Reputation: 13025
I'm wondering why you didn't get any compiler warning/error. You need to dereference your reference in the function:
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
The reason is i
and j
inside swapnum()
are addresses to original variables when the function is called. So when you use only i
or j
, you're getting the address of the variable, not the content. Here is an idea of what is going on:
int a = 10;
int b = 20;
-----------------
0x1000 | 10 | <-- a
-----------------
-----------------
0x1004 | 20 | <-- b
-----------------
swapnum(&a, &b);
Then, inside swapnum(int *i, int *j)
:
-----------------
0x2000 | 0x1000 | <-- i (*i == 10)
-----------------
-----------------
0x2004 | 0x1004 | <-- j (*j == 20)
-----------------
Upvotes: 4
Reputation: 10786
It's (hopefully) crashing because in this function:
void swapnum(int *i, int *j) {
int temp = i;
i = j;
j = temp;
}
i
and j
are pointers, and temp
is an int. You cannot assign a pointer to an int. If you want to swap the values in i
and j
do this:
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
Upvotes: 2
Reputation: 2622
In the swapnum
function, you are only assigning the variables i
and j
which are local to this function. This won't have any effect outside of this function. You should try:
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
Upvotes: 3
Reputation: 234807
In swapnum
, you are swapping the pointers (which are also int values). Try this instead:
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
Upvotes: 1
Reputation: 19443
You want:
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
Upvotes: 1