Click here to Skip to main content
15,398,575 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>

int swap(int *x, int *y);

int main()
{

  int x, y;
  x = 5;
  y = 7;
  printf("%d and %d \n", x, y);
  swap(&x, &y);
  printf("%d and %d \n", x,y);

  return 0;
}

int swap(int *x, int *y)
{

  int temp;
  *x = temp;\\ swap function 
  *x = *y;
  *y = temp;
  return 0;
}


What I have tried:

Please give solution as per the code module do not add something which is from another level
Posted
Updated 28-May-22 9:19am

1 solution

Your swap function is just wrong, the temp variable should be use to maintain the original value of x.
Try
C
#include <stdio.h>

void swap(int *x, int *y);

int main()
{

  int x, y;
  x = 5;
  y = 7;
  printf("%d and %d \n", x, y);
  swap(&x, &y);
  printf("swapped: %d and %d \n", x,y);

  return 0;
}

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900