Date création : 28-10-2007 00:00:00
 Vous êtes dans : Langage C Astuces / Exercices
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=%dn",a,b);
change(&a,&b);
printf("Apres : a=%d,b=%dn",a,b);
return 0;
}
void change(int *a,int *b){
*a += *b;
*b = *a-*b;
*a = *a-*b;
} |
Le résultat
lami20j@debian:~/trash$ gcc permuter_var.c
lami20j@debian:~/trash$ ./a.out
Avant : a=2,b=5
Apres : a=5,b=2 |
|