First of all, start explaining from mounting by the most basic function.
Naturally, use the argument of the pointer type because it is necessary to change the value of two variable.
Notes here : though it only has to exchange two variables on the side of the function now.
It is useless in the method.void swap(int *a,int *b) { *a = *b; *b = *a; return; }
The execution result of this program is as follows.#include <stdio.h> void swap(int *a,int *b); int main(void) { int a = 10,b = 100; printf("a = %3d : b = %3d\n",a,b); & nbsp;swap(&a,&b); printf("a = %3d : b = %3d\n",a,b); return 0; } void swap(int *a,int *b) { int temp; temp = *a; *a = *b; *b = temp; return; }
Though this of each type only has to be prepared now
a = 10 : b = 100
a = 100 : b = 10
It is mounting by the following addition and subtraction to be thought first of all.
The principle is easy. b is added first, and the way that preserves the value of b.#define SWAP(a,b) (a += b,b = a - b,a -= b)
It began to devise the method using another one and the exclusive-OR.
There is fundamentally no great difference though it omits when retreating not fixing the explanation of the exclusive-OR.#define SWAP(a,b) (a ^= b,b = a ^ b,a ^= b)
Though these two macros operate without trouble in case of almost
Actually, there is a case that doesn't operate well, and it becomes the maximum problem.
It happens when the same variable is specified.
Will actually try from the theory because it is evidence.
The execution result of this program is as follows.#include <stdio.h> #define SWAP(a,b) (a += b,b = a - b,a -= b) int main(void) { int a = 98; printf("a = %3d\n",a); SWAP(a,a); printf("a = %3d\n",a); return 0; }
If you exchanged the values of a that is the same variable though the result is [nahazu] naturally as it is
a = 98
a = 0
Though it might not be thought that there is what of the exchange of the same variables
Actually, with the sort program with array[i] When you exchanged array[j] It was i == j.
Then, be and get it. Moreover, most of the usage of SWAP is a sort program.
Generally though it doesn't depress any further because this problem is helpless
The author wants to solve this even forcibly, and is using the method by & operator.
In & operator, there is a character that the following expression is not executed if the expression executed immediately before is an imitation.
The following macro is SWAP macro using the character of & operator.
Execute the exchange in this macro only when the value of a and b is different.#define SWAP(a,b) ((a != b) && (a += b,b = a - b,a -= b))
Or, clause 3 operator can be used so as not to depend on the character of the operator.
Space cork of 0 in the latter half to dummy value for fixing up grammar.#define SWAP(a,b) ((a != b)? (a += b,b = a - b,a -= b) : 0 )
Though the SWAP macro can be made if the pointer variable is not used
Only the exchange of pointer variables has only mounting by the function shown first by all means.
And, pointers might be exchanged with the sort program unfortunately.
In addition, there is a possibility that drop out happens when the real number values are exchanged.
For instance, when the value of variable a is terribly huge, and the value of b is terribly minimum
The result is rounded even if a+b is calculated, and there is a possibility of becoming it as well as a.
Therefore, when accuracy is requested, it is not possible to use it.
After all, to exchange the values surely, the variable is temporarily needed by all means.
It is necessary to declare a variable in the macro for that.
If the type is specified, it is possible to declare a variable because the macro is only a mere replacement.
Moreover, the collision of the variable identifier can be avoided if it encloses and it blocks it.
Concretely, it becomes the following.
Though convenience is somewhat inferior to the extent that this macro should specify the type for the first argument#define SWAP(type,a,b) { type temp = a; a = b; b = temp; }
The following program is an example of exchanging variables by using this macro.
The execution result of this program is as follows.#include <stdio.h> #define SWAP(type,a,b) { type temp = a; a = b; b = temp; } int main(void) { int a = 10,b = 100; printf("a = %3d : b = %3d\n",a,b); SWAP(int,a,b) printf("a = %3d : b = %3d\n",a,b); return 0; }
Of course, when the same variable is specified, it operates without trouble.
a = 10 : b = 100
a = 100 : b = 10
At the end;The person of yucky : [wotsukerarenaino].
Because it is not concluded only by the macro as a sentence if it makes it#define swap(type,a,b) do{type _c;_c=a;a=b;b=_c;}while(0)
At the end;[Wotsukete] comes to operate.