somme et pointeur
2 participants
Page 1 sur 1
somme et pointeur
Slut,
Je fais appel à vous car j'ai un souci concernant le code suivant
Aidez moi SVP
Je fais appel à vous car j'ai un souci concernant le code suivant
- Code:
#include <stdio.h>
#include <stdlib.h>
long somme(long *pTableau[5])
{
int i;
int s;
for(i=0;i<5;i++)
{
s=s+ *pTableau[i];
}
return s;
}
int main()
{
int Tableau[5]={1,1,2,3,4};
int *pTableau[5];
pTableau[5]=&Tableau[5];
int resultat;
resultat=somme(pTableau[5]);
printf("la somme du tableau est %d:",resultat);
return 0;
}
Aidez moi SVP
potter- Bavard
- Messages : 18
Date d'inscription : 24/11/2008
Re: somme et pointeur
Oui, le compilateur indique un certain nombre d'anomalies qu'il convient de corriger avant tout :potter a écrit:Slut,
Je fais appel à vous car j'ai un souci concernant le code suivant
- Code:
-------------- Build: Debug in hello ---------------
Compiling: main.c
Linking console executable: bin\Debug\hello.exe
C:\dev\hello\main.c: In function `somme':
C:\dev\hello\main.c:8: warning: 's' might be used uninitialized in this function
C:\dev\hello\main.c: At top level:
C:\dev\hello\main.c:16: warning: function declaration isn't a prototype
C:\dev\hello\main.c: In function `main':
C:\dev\hello\main.c:24: warning: passing arg 1 of `somme' from incompatible pointer type
Output size is 19.58 KB
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 3 warnings
http://www.bien-programmer.fr/codage.htm#cfg_compilo
Je recommande la lecture de cet article :
http://www.bien-programmer.fr/notes.htm#param_tab
Ton code corrigé :
- Code:
#include <stdio.h>
#include <stdlib.h>
int somme (int Tableau[5], int n)
{
int i;
int s = 0;
for (i = 0; i < n; i++)
{
s = s + Tableau[i];
}
return s;
}
int main (void)
{
int Tableau[5] = { 1, 1, 2, 3, 4 };
int resultat;
resultat = somme (Tableau, 5);
printf ("la somme du tableau est %d\n", resultat);
return 0;
}
Re: somme et pointeur
Merci beaucoup ed, ça marche
potter- Bavard
- Messages : 18
Date d'inscription : 24/11/2008
Page 1 sur 1
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum
|
|