In your program 1, the addresses of two parameters x and y are passed in. The two parameters IIIy point to only two 8-bit char. When swapping temporary variables with XOR (swapIntOrChar), the contents pointed by ix and iy are interpreted as 32-bit int through implicit type conversion, which means that XOR operation is the 32-bit variable content of operation, which has crossed the line. Different compilers and different variable assignments may have different results, and generally there will be no debug crash.
SwapIntOrChar in program 2 uses Char for forced type conversion, which is the original type pointed by ix iy, so there will be no problem.
I wrote you an example of exchanging int or char, which can be realized by template:
Template & lttypename T>
void swapIntOrChar(T *lpX,T *lpY)
{
*lpx^=*lpy,*lpy^=*lpx,*lpx^=*lpy;
}
Use as follows:
char x='a ',y = ' b
swapIntOrChar(& amp; x & amp; y);
int a= 1,b = 2;
swapIntOrChar(& amp; I. & AMPB);
Vc is compiled as follows.