Monday, May 24, 2010

C code problem?

#include%26lt;stdio.h%26gt;


void swapr(int, int);


void main()


{


int a=10,b=20;


swapr(%26amp;a,%26amp;b);


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





}


void swapr(int*x,int *y)


{


int t;


t=*x;


*x=*y;


*y=t;


}


And error messages are


Compiling CAL_REF.CPP:


Error CAL_REF.CPP 6: Cannot convert 'int *' to 'int' in function main()


Error CAL_REF.CPP 6: Type mismatch in parameter 1 in call to 'swapr(int,int)' in function main()


Error CAL_REF.CPP 6: Cannot convert 'int *' to 'int' in function main()


Error CAL_REF.CPP 6: Type mismatch in parameter 2 in call to 'swapr(int,int)' in function main()

C code problem?
You made a mistake on the second line. You declared swapr as a function that takes two ints and returns nothing, but then you define swapr as a function that takes to pointers to ints and returns nothing.





So change the second line from


:: void swapr(int, int);


to


:: void swapr(int*, int*);
Reply:Try defining the variable 't' as integer pointer instead of integer.
Reply:you should just change the second line to void swapr(int*,int *)
Reply:#include%26lt;stdio.h%26gt;


void swapr(int, int); %26lt;- This is where the problem lies, you have declared a function that accepts two integer arguments where as you need to declare it as


void swapr(int *, int *); so it matches the function definition below the function main().


void main()


{


int a=10,b=20;


swapr(%26amp;a,%26amp;b);


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


}





void swapr(int*x,int *y)


{


int t;


t=*x;


*x=*y;


*y=t;


}


No comments:

Post a Comment