Home > Back-end >  Indirect access operations (* p) using xor operations exchange error has pointed out the value of a
Indirect access operations (* p) using xor operations exchange error has pointed out the value of a

Time:04-22

RT, the program is as follows:
# include
Int main (void)
{
Int a=1, b=2;
P1, int * * p2.

P1=& amp; A, p2=& amp; b;
Printf (" a=% d, b=% d, * p=% d, * p2=% d \ n ", a, b, * p1, * (p2);
* p1 p2 ^ ^=*=* p1 ^=* (p2);
Printf (" a=% d, b=% d, * p=% d, * p2=% d \ n ", a, b, * p1, * (p2);

return 0;
}

The output is:
A=1, b=2, * p=1, * p2=2
A=0, b=1, * p1=0, * p2=1

Again using the Boolean xor * after the values of p1 and p2 are not successful exchange, * p1 value becomes 0, with bosses explain why

CodePudding user response:

 # include 
Int main (void)
{
Int a=1, b=2;
P1, int * * p2.

P1=& amp; A, p2=& amp; b;
Printf (" a=% d, b=% d, * p=% d, * p2=% d \ n ", a, b, * p1, * (p2);
# if 0
Printf (" No. 1: % d \ n ", (* ^ p1=* (p2));
Printf (" % d \ n ", * (p1);
Printf (" No. 2: % d \ n ", p2 ^=(* * (p1));
Printf (" % d \ n ", * (p2);
Printf (" No. 2: % d \ n ", p1 ^=(* * p2));
Printf (" % d \ n ", * (p1);
# the else
* p1 p2 ^ ^=*=* p1 ^=* (p2);
# endif
Printf (" a=% d, b=% d, * p=% d, * p2=% d \ n ", a, b, * p1, * (p2);

return 0;
}

For your reference ~

The if and the else are all the same, the else part decomposition version is the performance of the if 0;

Results:
 a=1, b=2, * p=1, * p2=2 
A=2, b=1, * p=2, * p2=1

CodePudding user response:

The
reference 1/f, confident boy reply:
 # include 
Int main (void)
{
Int a=1, b=2;
P1, int * * p2.

P1=& amp; A, p2=& amp; b;
Printf (" a=% d, b=% d, * p=% d, * p2=% d \ n ", a, b, * p1, * (p2);
# if 0
Printf (" No. 1: % d \ n ", (* ^ p1=* (p2));
Printf (" % d \ n ", * (p1);
Printf (" No. 2: % d \ n ", p2 ^=(* * (p1));
Printf (" % d \ n ", * (p2);
Printf (" No. 2: % d \ n ", p1 ^=(* * p2));
Printf (" % d \ n ", * (p1);
# the else
* p1 p2 ^ ^=*=* p1 ^=* (p2);
# endif
Printf (" a=% d, b=% d, * p=% d, * p2=% d \ n ", a, b, * p1, * (p2);

return 0;
}

For your reference ~

The if and the else are all the same, the else part decomposition version is the performance of the if 0;

Results:
 a=1, b=2, * p=1, * p2=2 
A=2, b=1, * p=2, * p2=1


I ran your code, the results did not change, as well as the original,,
  • Related