Bonjour
Je commence le C et je suis rendu au tableau avec les pointeurs et j'ai un petit souci.
Je regroupe toute les fonctions dans un meme fichier ou on peut choisir laquelle executer avec un menu (par exemple 1 : Calculer la somme du tableau 2: la moyenne etc...)
J'ai un souci au niveau des pointeurs mais je ne vois pas où.
Je vous met mon code
Quand je compile et execute ce programme, il marche bien (enfin ca a l'air)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 #include <stdio.h> #include <stdlib.h> static long tailletableau; static long *pointeurtaille; static long *pointeurtableau; int menu() //menu pour le choix des fonctions a executer sur le tableau { int choix; printf("==== MENU ====\n\n"); printf("1 : Somme des valeurs du tableau\n"); printf("2 : Moyenne des valeures du tableau\n"); scanf("%d",&choix); return choix; } void init () //initialisation du tableau { long i; printf("Rentrez la taille du tableau : "); scanf("%ld",&tailletableau); long tableau[tailletableau]; for (i = 0 ; i < tailletableau ; i++) { printf("Rentrez les valeurs du tableau : "); scanf("%ld", &tableau[i]); } pointeurtableau = &tableau; pointeurtaille = &tailletableau; } long sommetableau(long tableau[], long tailletableau) { long somme = 0; long i; for(i=0;i<tailletableau;i++) { somme = somme + tableau[i]; } return somme; } void affiche(long *tableau, long tailletableau) { long i; for (i = 0 ; i < tailletableau ; i++) { printf("%ld\n", tableau[i]); } } int main() { init(); //printf("%ld\n",pointeurtableau[0]); en rajoutant ces 2 lignes, ca plante (voir le probleme plus bas) //printf("%ld\n",pointeurtaille); printf("La somme du tableau vaut %ld", sommetableau(pointeurtableau, *pointeurtaille)); return 0; }
Puis je veux mettre en application le menu avec un switch.
Et la, quand j'execute il me rend une valeur totalement fausse (du genre 24564423)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 #include <stdio.h> #include <stdlib.h> static long tailletableau; static long *pointeurtaille; static long *pointeurtableau; int menu() { int choix; printf("==== MENU ====\n\n"); printf("1 : Somme des valeurs du tableau\n"); printf("2 : Moyenne des valeures du tableau\n"); scanf("%d",&choix); return choix; } void init () { long i; printf("Rentrez la taille du tableau : "); scanf("%ld",&tailletableau); long tableau[tailletableau]; for (i = 0 ; i < tailletableau ; i++) { printf("Rentrez les valeurs du tableau : "); scanf("%ld", &tableau[i]); } pointeurtableau = &tableau; pointeurtaille = &tailletableau; } long sommetableau(long tableau[], long tailletableau) { long somme = 0; long i; for(i=0;i<tailletableau;i++) { somme = somme + tableau[i]; } return somme; } void affiche(long *tableau, long tailletableau) { long i; for (i = 0 ; i < tailletableau ; i++) { printf("%ld\n", tableau[i]); } } int main() { init(); switch (menu()) { case 1: printf("La somme du tableau vaut %ld", sommetableau(pointeurtableau, *pointeurtaille)); break; default: printf("pas bon"); break; } return 0; }
Si quelqu'un a une réponse a mon problème, je lui serai reconnaissant!!!
Partager