#include <stdio.h>
void swap1(int, int);
void swap2(int *, int *);
void swap1(int a, int b) {
int c;
c = a;
a = b;
b = c;
}
void swap2(int * a, int * b) {
int c;
c = *a;
*a = *b;
*b = c;
}
int main(void) {
int a, b;
a = 1; b = 2;
printf("a = %d, b = %d\n", a, b);
printf("appel de la fonction swap1(a, b)\n");
swap1(a, b);
printf("a = %d, b = %d\n", a, b);
printf("*************************\n");
a = 1; b = 2;
printf("a = %d, b = %d\n", a, b);
printf("appel de la fonction swap2(&a, &b)\n");
swap2(&a, &b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
|
|
-bash-2.05b$ gcc -Wall swap01.c -o swap01
-bash-2.05b$ ./swap01
a = 1, b = 2
appel de la fonction swap1(a, b)
a = 1, b = 2
*************************
a = 1, b = 2
appel de la fonction swap2(&a, &b)
a = 2, b = 1
Pointeurs et paramètres de fonction.
|
|