Surement sur les pointeurs
Bonjour ou plutot Bonsoir a cette heure-ci, je suis tout nouveau ici =)), c'est un ami qui ma fais decouvrir ce site, et je trouve qu'il ma beaucoup aidé.
Donc j'expose mon probleme:=> Debutant que je suis,j'ai voulu concevoir un pti premier programme sur le pendu, ainsi j'ai etudié les pointeurs
Voici mon code, et excusez moi d'avance si mon programme n'est pa tres propre
Code:
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
char * init_mot(){
srand(time(NULL));
int i=0;
char *p_mot ;
char *tab[3]={"bonjour","aurevoir","abientot"};
p_mot = tab[rand()%3];
/* for ( i=0; i<strlen(p_mot);i++){
printf("%c",p_mot[i]);
}
*/
return p_mot;
}
char * init_jeu(char *mot){
char *p_devine;
char devine[32];
int longueur;
int i=0;
p_devine = &devine;
longueur = strlen(mot);
printf("Voici le mot que tu dois deviner ===>\t");
for(i=0;i<longueur;i++){
devine[i]='_';
printf("%c",devine[i]);
}
printf("\n");
return p_devine;
}
void jouer(char *mot, char *devine, int longueur){
int i=0; // variable de boucle
char lettre; // variable lettre entre
int nb=0; // variable lettre presente ds le mot
int err=3; // cpt d erreur
int var=0; // cpt gain
/* for (i=0;i<longueur;i++){
printf("%c",devine[i]);
}
*/
do
{
nb=0;
printf("\n rentre une lettre\n");
scanf("%c",&lettre);
getchar();
//printf("tu as rentré un %c\n",lettre);
for(i=0;i<longueur;i++){
if(mot[i] == lettre){
*(mot+i)=*(devine+i);
*(devine+i)=lettre;
nb++;
//printf("%c",mot[i]);
}
}
var+=nb;
if(nb == 0 ){
err--;
printf("Il te reste encore %d chances\n",err);
}
for (i=0;i<longueur;i++){
printf("%c",devine[i]);
}
}
while(var<longueur && err>0);
if(err == 0)
printf("T une chips \n");
else
printf("T un winner\n");
}
int main(){
char *devine;
char *mot;
mot = init_mot();
printf("\n");
devine = init_jeu(mot);
int longueur= strlen(mot);
//printf("%d",longueur);
jouer(mot,devine,longueur);
return 0;
} |
J'ai testé mon programme et mon probleme est que dans ma fonction jouer a l'exterieur de mon do while mes pointeurs pointent bien sur la bonne zone memoire en revanche a l'interieur mon pointeur pointe sur n'importe koi ?_?
Voila j'espere j'ai été assez clair dans mes propos en esperant un peti coup de pouce de votre part merci d'avance
Re: Bonjour Debutant Need Help Plz (surement sur les pointeu
Citation:
Envoyé par PimsOrange
Voici mon code, et excusez moi d'avance si mon programme n'est pa tres propre
Ton code ne compile pas. et il rend mon compilateur très bavard :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
Compiling: main.c
main.c:6: warning: function declaration isn't a prototype
main.c: In function `init_mot':
main.c:8: warning: passing arg 1 of `srand' as unsigned due to prototype
main.c:12: warning: initialization discards qualifiers from pointer target type
main.c:12: warning: initialization discards qualifiers from pointer target type
main.c:12: warning: initialization discards qualifiers from pointer target type
main.c:9: warning: unused variable `i'
main.c: At top level:
main.c:23: warning: no previous prototype for 'init_jeu'
main.c: In function `init_jeu':
main.c:30: warning: assignment from incompatible pointer type
main.c:31: error: implicit declaration of function `strlen'
main.c:31: warning: nested extern declaration of `strlen'
<internal>:0: warning: redundant redeclaration of 'strlen'
main.c: At top level:
main.c:43: warning: no previous prototype for 'jouer'
main.c: In function `main_':
main.c:108: warning: nested extern declaration of `strlen'
<internal>:0: warning: redundant redeclaration of 'strlen'
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 11 warnings |