user1261852
user1261852

Reputation:

Why does a swap function only work when using pointers?

Here is an example of what I mean. This example is without using pointers and the variables do not swap.

void  swap(int x, int y);
main()
{
    int a = 33, b = 55;
    swap (a, b);
    printf("a = %d, b = %d\n", a, b);
}

void swap(int x, int y)
{
    int temp;
    temp = x;
    x = y;
    y = temp;
}

Now if you use pointers the variables a and b swap. Why is it that it only works with pointers?

Upvotes: 0

Views: 2483

Answers (4)

WhozCraig
WhozCraig

Reputation: 66194

C is a pass-by-value language. Always. That means with this:

void swap(int x, int y)
{
    int temp = x;
    x = y;
    y = temp;
}

you're swapping the values of x and y, obtained from the values passed to the function. When you invoke like this:

int main()
{
    int a = 33, b = 55;
    swap (a, b); // passing values of a and b
    printf("a = %d, b = %d\n", a, b);
}

the values of a and b are passed. So this is your problem. The solution is to make the values passed something that can be used to modify the caller's variables. If you want to modify those variables they need to somehow be addressable from the called code. Hmmm...

The mechanism is called "pass by address", and though it sounds fancy in reality it isn't. It is simply a retooling of pass-by-value, but with a different value type. Whereas before we were passing values of type int we will instead pass addresses of the integer variables and declare the formal parameters to be pointers to int instead. Make no mistake. They're still values, but not of the basic int type. Rather the values passed are addresses of int variables. To access the data at those addresses pointers are used in conjunction with the dereference operator (of which there are several kinds, only one shown here):

void swap(int *ptrToX, int *ptrToY)
{
    int temp = *ptrToX; // dereference right, store value in temp
    *ptrToX = *ptrToY;  // dereference both, assigning value from right to left.
    *ptrToY = temp;     // dereference left, assign temp value
}

and invoked like this:

int main()
{
    int a = 33, b = 55;
    swap (&a, &b); // passing addresses of a and b
    printf("a = %d, b = %d\n", a, b);
}

Note: Often ill-quoted as the exception to pass-by-value is passing an array. Though the phrase "decays to a pointer" is thrown about like confetti on New Years Eve, I abhor that vernacular. The verb itself implies a functional operation where there is, in fact, none.

The language specifies the value of an array used in an expression is the address of its first element. In other words, its "value" is already an address and as such can simply be passed by-value to a function expecting a pointer to the same base type. Because it is an address, the receiving parameter (a pointer, because thats what holds address values) can then be used to modify the array content from the caller. Second only to multiple levels of indirection (pointers to pointers, etc) it is easily the hardest thing for people new to C to wrap their head around, yet it is important you do so.

Upvotes: 3

Sorcrer
Sorcrer

Reputation: 1644

This is the best example to define the advantage/use of function call by reference V/s call by value

In the main() function the two variables has the value

int a = 33, b = 55;

so using these variables in the swap(a,b) function, it just passes the values to the function definition where

void swap(int x, int y)
{
    int temp;
    temp = x;
    x = y;
    y = temp;
}

they declare again two variables x,y and the values in this variables are swapped. But the variables x and y are only known to function swap() after execution of the swap() function that variables are no longer valid. So nothing happens to the variables a,b in the main function.

So in order to swap variables using function you've to use

swap(&a,&b);

in main() function , and modify function swap()

void swap(int *x, int *y)
{
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

Which passes the address of variables a and b , and the pointers are used to swap the variables,It gets swapped effectively. Since pointer points to the address of the variables the value of the variables get changed.

For more google "call by referece and call by value in c"

Upvotes: 0

brokenfoot
brokenfoot

Reputation: 11609

That's called pass by value.
You are just passing values of a & b to the swap function. The swap function copies that value into its variables x & y which are different from a & b. Hence in swap you are working on x & y instead of a & b.

On the other hand, when you pass pointers, you are passing the address where a & b are stored. Your x & y point to the same address and hence you are manipulating the content at that address where a & b are stored. Hence, main gets to see the updated values.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206577

When you call swap, the value of the variables a and b are passed to it. These values are passed by copy. When you swap those values in swap, you are only swapping local copies of a and b.

When you pass pointers to variables to a function, you are able to modify the values of the variables in the calling function.

Upvotes: 1

Related Questions