Swap two values of variables

时间:2016-10-20 19:49:15

标签: c swap

I try to swap values of two variables in C, I wrote function but it doesnt work and I cant figure it out whats wrong.

#include <stdio.h>

void fun(int a_local, int b_local)
{
    int temp = a_local;
    a_local = b_local;
    b_local = temp;
}

int main()
{
    int a_global = 5;
    int b_global = 7;

    printf("a=%d, b=%d\n", a_global, b_global);
    fun(a_global, b_global);
    printf("a=%d, b=%d\n", a_global, b_global);

    return 0;
}

5 个答案:

答案 0 :(得分:2)

In C, all function parameters are pass by value. So changes made to a parameter inside the function are not visible to the calling function.

You need to change the function to accept a pointer for each variable you want to change. Then you pass the address of those variables. Then in the function, you dereference the pointers to change the variables in the calling function.

#include <stdio.h>

void fun(int *a_local, int *b_local)
{
    int temp = *a_local;
    *a_local = *b_local;
    *b_local = temp;
}

int main()
{
    int a_global = 5;
    int b_global = 7;

    printf("a=%d, b=%d\n", a_global, b_global);
    fun(&a_global, &b_global);
    printf("a=%d, b=%d\n", a_global, b_global);

    return 0;
}

答案 1 :(得分:0)

The function is void and not returning anything, instead you can use ampersand when passing in the arguments "Pass by reference" and all "Changes" will be seen after the function call.

#include <stdio.h>

void fun(int* a_local, int* b_local)
{
    int temp=*a_local;
    *a_local=*b_local;
    *b_local=temp;
}
int main()
{
    int a_global=5;
    int b_global=7;

    printf("a=%d, b=%d\n", a_global, b_global);
    fun(&a_global,&b_global);
    printf("a=%d, b=%d\n", a_global, b_global);

return 0;
}

答案 2 :(得分:0)

The problem on your function is that the values are being passed by value, not by reference, therefore the swap is properly done but is being lost in RAM when returning from the function (both internal variables are local to the function and destroyed on exit)

here is one solution: use pointers.

void fun(int* a_local, int* b_local)
{
    int temp=*a_local;
    *a_local=*b_local;
    *b_local=temp;
}

答案 3 :(得分:0)

You need to pass the variables as pointers to the swapping function, otherwise their local copies get created when you call the function, values of these local copies are swapped and then destroyed when you return from the function, leaving your original variables unchanged.

void fun(int* a_local, int* b_local)
{
    int temp=*a_local;
    *a_local=*b_local;
    *b_local=temp;
}
int main()
{
    int a_global=5;
    int b_global=7;

    printf("a=%d, b=%d\n", a_global, b_global);
    fun(&a_global,&b_global);
    printf("a=%d, b=%d\n", a_global, b_global);

return 0;
}

答案 4 :(得分:0)

To do it your way you would need to return the values from the "fun" function, or they won't actually be changed in the main. To get around this you could not do it in a function, like this example..

int main()
{
    int a_global=5;
    int b_global=7;

    printf("a=%d, b=%d\n", a_global, b_global);
    int temp=a_local;
    a_local=b_local;
    b_local=temp;
    printf("a=%d, b=%d\n", a_global, b_global);

return 0;
}

You could also put the two values into a struct, and use that to pass them in and out of fun.