Thursday, July 30, 2009

Why is this C++code wrong?

int main()


{


int x=10;


int y=20;


cout%26lt;%26lt;*(swap(x,y));


return 0;





}


int *swap(int x, static y)


{


int t=y;


y=x;


x=t;


return %26amp;y;


}


shouldn't it print 10 in the output?


when I compile it I recieve these errors and warnings:


error C2100: illegal indirection


error C2679: binary '%26lt;%26lt;' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)


warning C4042: 'y' : has bad storage class


warning C4172: returning address of local variable or temporary

Why is this C++code wrong?
This looks like an intentionally wrong program to test C++ knowledge. It doesn't make much sense, except perhaps help to understand C++ compiler errors.





Several things are wrong.


1) C4042 - Parameters of functions can't be "static" - "static y" doesn't make any sense - it should be juse "int y"





2) C4172 - you can't return address of local variable because by the time when execution of function swap() is finished, variable y no longer exists. Declaring it static is not going to help. Most sane compilers will ingore "static" here.





3) C2100 and C2679 errors are caused by the fact that function swap() is not know to the compiler at the point in source when it complies main().


It assumes that swap is defined as void function declared elsewhere, therefore you can't do * (dereference) on void and you cant feed it into cout.





If intention is to swap values of two variables in memory, the program would have to be re-written like this:








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





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





int main()


{


int x=10;


int y=20;


cout%26lt;%26lt;*swap(%26amp;x,%26amp;y);


return 0;





}


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


{


int t=*y;


*y=*x;


*x=t;


return y;


}
Reply:There is so much wrong with that code its beyond help. It looks like your using a lot of things you don't understand.





Go back to your C++ textbook, read it, and try again. More importantly stop trying to write fancy code you don't understand. It will get you no where.
Reply:Well, you got the errors. Did you write this? First, do you have a function prototype? Second, static y for your second parameter. What's the type? A parameter static doesn't really make sense. First, it doesn't have a type. It is also very bad form to return references to local types, which is what parameter is. If a parameter is a passed by reference, then you really don't need to return it. I am not entirely sure what this code is trying to accomplish.





Why all the pointers?





#include %26lt;iostream%26gt;





using namespace std;





int swap(int%26amp; x, int%26amp; y);





int main()


{


int x=10;


int y=20;


cout%26lt;%26lt; swap(x,y);


return 0;





}


int swap(int%26amp; x, int%26amp; y)


{


int t=y;


y=x;


x=t;


return y;


}
Reply:The main problem is in the swap function. 'static y' is a bad storage class. It needs to be an int, int* or int%26amp;.





swap substitutes one value for another, but why is it returning a ponter to just one value? Wouldn't the caller want both values? That's what the name swap implies.





void swap(int%26amp; x, int%26amp; y)


{


int t=y;


y=x;


x=t;


}





This modifies the actual integers that the caller passes in. So main would be:





int main()


{


int x=10;


int y=20;


swap(x, y);


cout%26lt;%26lt;"x is "%26lt;%26lt; x %26lt;%26lt;" y is "%26lt;%26lt; y;





return 0;





}





Passing by reference means that the function can modify the actual int that the caller passes in. If you pass by value (just int x, int y), then the function changes only it's local, temporary copy of the int. The caller's copy of the int is unaffected.





You could also pass int* instead of int%26amp;, but you're in C++, so you should use a reference.





Functions should never return a pointer to a local variable because that variable is temporary and will be gone as soon as the function returns. The caller will be left with a pointer to "nothing" (actually, some place in memory that should not be accessed). This is the reason for your last compiler warning.
Reply:You should use x and y in ur formal parameter.Try main() int x=20,y=10 cout%26lt;x'/t'%26lt;y; cout%26lt;'calling swap'; int swap(int %26amp;a,int %26amp;b) {int t=a;a=b;b=t; cout%26lt;/n%26lt;a%26lt;/t%26lt;b;} the punctuations are wrong but it is the right idea.


No comments:

Post a Comment