Browse Source

Fin TP4 partie 2

master
Elias Leinenweber 6 months ago
parent
commit
b3eed7f892
  1. 25
      TP4/part2_counterexamples/examples.c

25
TP4/part2_counterexamples/examples.c

@ -1,9 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
int *alloc(int);
void swap(float, float);
void swap(float *, float *);
int
main(int argc, char *argv[])
@ -15,21 +16,25 @@ main(int argc, char *argv[])
/* use printf to look at the content accessible from ptr */
for (i = 0; i < SIZE; ++i)
printf("ptr[%d] = %d\n", i, ptr[i]);
/* don't forget to free ptr after use */
free(ptr);
f1 = 0.7;
f2 = 1.2;
/* use swap to put value of f1 in f2 and vice-versa */
swap(f1, f2);
swap(&f1, &f2);
/* use printf to display values of f1 and f2 */
printf("f1 = %d, f2 = %d\n");
return 0;
printf("f1 = %.1f, f2 = %.1f\n", f1, f2);
return EXIT_SUCCESS;
}
int *
alloc (int val)
alloc(int val)
{
int i, tab[SIZE];
int i, *tab;
tab = malloc(SIZE * sizeof val);
/* initialize elements of tab with value val */
for (i = 0; i < SIZE; ++i)
tab[i] = val;
@ -38,11 +43,11 @@ alloc (int val)
}
void
swap(float f1, float f2)
swap(float *f1, float *f2)
{
float tmp;
tmp = f1;
f1 = f2;
f2 = tmp;
tmp = *f1;
*f1 = *f2;
*f2 = tmp;
}

Loading…
Cancel
Save