-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
42 lines (36 loc) · 866 Bytes
/
test.c
File metadata and controls
42 lines (36 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
int *tableauScanfTaille(int taille) {
int *tableau = malloc(taille * sizeof *tableau);
if (tableau == NULL) {
fprintf(stderr, "Erreur allocation mémoire échouée\n");
exit(1);
}
for (int i = 1; i < taille+1; i++) {
printf("Entrez la valeur pour l'élément %d: \n", i);
if (scanf("%d", &tableau[i]) != 1) {
fprintf(stderr, "Erreur valeur invalide\n");
free(tableau);
exit(1);
}
}
return tableau;
}
void afficherTab(int* tab, int taille) {
printf("{");
for (int i = 0; i < taille; i++) {
printf("%d", tab[i]);
if (i < taille - 1) {
printf(", ");
}
}
printf("}\n");
}
int main() {
int taille = 5;
printf("Entrez les elements: \n");
int* tableau = tableauScanfTaille(taille);
afficherTab(tableau, taille);
free(tableau);
return 0;
}