Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C help on figuring out swap function

Jhon

New Coder
I have two functions to swap 2 numbers. swap1 and swap2. swap1 but but not swap2.
Can any one help me why swap2 is not working?
I appreciate that.
Jhon.

#include <stdio.h>
#include <stdlib.h>
void swap1 (int* A, int* B);
void swap2 (int A, int B);
int main()
{
int num1 = 10; int num2 = 3;
printf ( " Before swap1: num1 = %d , num2 = %d \n", num1,num2);
swap1(&num1, &num2);
printf ( " After first swap1: num1 = %d , num2 = %d \n", num1,num2);

printf("---------------------------------------------------------\n\n");

int num3 = 10; int num4 = 3;
printf ( " Before swap2: num3 = %d , num4 = %d \n", num3,num4);
swap2(num3, num4);
printf ( " After first swap2: num3 = %d , num4 = %d \n", num3,num4);
return 0;
}
void swap1 (int* A, int* B)
{
int C = 0;
C = *A;
*A = *B;
*B = C;
}
void swap2 (int A, int B)
{
int C = 0;
int D = 0;
C = A;
D = B;
A = D;
B = C;
}
 
Back
Top Bottom