Permuter deux variables sans utilisation d'une variable temporaire
Le code
#include <stdio.h>
void change(int *,int*);
int main ()
{
int a=2,b=5;
printf("Avant : a=%d,b=%d\n",a,b);
change(&a,&b);
printf("Apres : a=%d,b=%d\n",a,b);
return 0;
}
void change(int *a,int *b){
*a += *b;
*b = *a-*b;
*a = *a-*b;
}
Le résultat
Avant : a=2,b=5
Apres : a=5,b=2