4
#include "stdio.h"
#include "conio.h"

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

void main()
{
int a=10,b=20;
swap(a,b);
printf("value of a=%d and b=%d");
getch();
}

void swap(int *x,int *y)

{
  if(x!=y)
     {
      *x ^= *y;
         *y ^= *x;
         *x ^= *y;

     }
}

// I'm getting .. cann't convert int to int * ...

can anybody tell me why so . and how to solve it regards.

hoping for quick and positive response.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
  • 1
    Your call to `printf()` is also missing arguments. It should be `printf("value of a=%d and b=%d", a, b);`. – Jonathan Grynspan Sep 06 '10 at 19:46
  • 3
    Apart from your problem at hand, some remarks: conio is not a standard header, stdio should not be included via double quotes and void main is not in the standard, either. While it may work in your given toolchain, writing portable and standard conformant code is probably a good idea. – Jim Brissom Sep 06 '10 at 20:06
  • Which language are you aiming for? Don't just tag language tags on. And [get a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), sounds like you need to learn fundamentals. – GManNickG Sep 06 '10 at 23:27
  • Unrelated to the question, you should not be using that swap algorithm unless you know full well what it does and why. You should read up about it: http://en.wikipedia.org/wiki/Xor_swap – Itai Ferber Sep 07 '10 at 00:13
  • 15.6k score and such simple mistake? – i486 Mar 02 '17 at 21:36
  • And the XOR hack fails catastrophically if a and b are aliases. Just use a temporary. – Malcolm McLean Mar 02 '17 at 22:21

1 Answers1

21

Your call to swap() should include ampersands:

swap(&a,&b);

swap is expecting pointers to int, so you need to take a and b's addresses when passing them in.

Graham Perks
  • 23,007
  • 8
  • 61
  • 83