InterviewSolution
Saved Bookmarks
| 1. |
What is the issue with the following piece of code? |
|
Answer» int square (volatile int *p){ return (*p) * (*p) ;} From the code GIVEN, it appears that the function intends to return the square of the VALUES pointed by the pointer p. But, since we have the pointer point to a volatile integer, the compiler GENERATES code as below: int square ( volatile int *p){ int x , y; x = *p ; y = *p ; return x * y ;}Since the pointer can be changed to point to other locations, it might be possible that the values of the x and y WOULD be different which might not even result in the square of the numbers. Hence, the correct way for ACHIEVING the square of the number is by coding as below: long square (volatile int *p ){ int x ; x = *p ; return x*x;} |
|