Votre IP: 38.107.179.241 
  
 
Google
 
Accueil e-mail Linux
Linux
Perl
Perl
C
Langage C
Dico
Dictionnaire
Biblio liens
Bibliothèque de liens
Index articles
Index articles
 

Date création : 28-10-2007 00:00:00

C  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

Création : octobre 2007  © Tous droits réservés 2007 linux-perl-c
Valid HTML 4.01 TransitionalValid CSS