Programme en c
Résolu
sofi
-
sofi -
sofi -
Bonjour,
j'ai le programme suivant qui normalement me permet d'afficher une piéce de tetrice représanté par des chaines de caractères mais il y a des bug : est ce voous pouvez m'aider s'il vous palit:
mon code est le suivant:
et le code piece.h est le suivant
et laliste des fautes sont :
piece.c:8:14: warning: assignment makes integer from pointer without a cast
piece.c:10:13: warning: assignment makes integer from pointer without a cast
piece.c:11:2: error: expected identifier or `(' before `.' token
piece.c:12:2: error: expected identifier or `(' before `.' token
piece.c:13:2: error: expected identifier or `(' before `.' token
piece.c: In function `initializer':
piece.c:17:3: error: `piece' undeclared (first use in this function)
piece.c:17:3: note: each undeclared identifier is reported only once for each function it appears in
piece.c:17:9: error: expected `;' before `P'
piece.c:18:2: error: expected identifier or `(' before `.' token
piece.c:19:2: error: expected identifier or `(' before `.' token
piece.c: In function `afficher':
piece.c:23:1: error: `piece' undeclared (first use in this function)
piece.c:23:7: error: expected `;' before `P'
piece.c:25:13: error: expected expression before `P'
piece.c:26:15: error: expected expression before `P'
j'ai le programme suivant qui normalement me permet d'afficher une piéce de tetrice représanté par des chaines de caractères mais il y a des bug : est ce voous pouvez m'aider s'il vous palit:
mon code est le suivant:
piece.c #include<stdio.h> #include"piece.h" int main() { int i,j; char tab [3][2] ; for(i=0;i<2;i++) tab[i][0]="@"; for(j=1;j<3;j++) tab[i][j]="@"; P.forme=tab; P.initializer(3,2,P.forme,P); P.afficher(P); return 0; } void initializer(int n, int p, char tableau[4][4]) { piece P; P.hauteur_piece=n; P.largeure_piece=p; } void afficher() { piece P; int i,j; for( i=0;i<=P.hauteur_piece;i++) for(j=0;j<=P.largeure_piece;i++) { printf("%c",P.forme[i][j]); } }
et le code piece.h est le suivant
#include<stdio.h> struct piece { int hauteur_piece; int largeure_piece; char forme[4][4]; }; typedef struct piece P; void initializer(int n, int p, char tableau[4][4]); void afficher();
et laliste des fautes sont :
piece.c:8:14: warning: assignment makes integer from pointer without a cast
piece.c:10:13: warning: assignment makes integer from pointer without a cast
piece.c:11:2: error: expected identifier or `(' before `.' token
piece.c:12:2: error: expected identifier or `(' before `.' token
piece.c:13:2: error: expected identifier or `(' before `.' token
piece.c: In function `initializer':
piece.c:17:3: error: `piece' undeclared (first use in this function)
piece.c:17:3: note: each undeclared identifier is reported only once for each function it appears in
piece.c:17:9: error: expected `;' before `P'
piece.c:18:2: error: expected identifier or `(' before `.' token
piece.c:19:2: error: expected identifier or `(' before `.' token
piece.c: In function `afficher':
piece.c:23:1: error: `piece' undeclared (first use in this function)
piece.c:23:7: error: expected `;' before `P'
piece.c:25:13: error: expected expression before `P'
piece.c:26:15: error: expected expression before `P'
A voir également:
- Programme en c
- Programme demarrage windows - Guide
- Mettre en veille un programme - Guide
- Message programmé iphone - Guide
- Programme word gratuit - Guide
- Cette action ne peut pas être réalisée car le fichier est ouvert dans un autre programme - Guide
1 réponse
C'est normal, tu essais de mettre une chaine de caractère dans un caractère. Remplace "@" (qui est une chaine) par '@' (qui est un caractère).
Ensuite, typedef te défini un type pas une variable. Enlève le typedef (mais pas ce qu'il y a derrière) ça devrais aller mieux.
Pour finir a=b si a et b sont des tableaux NE recopie PAS le tableaux b dans a. Il peut le faire, mais ce n'est pas standard.
Dit nous si tu as encore des erreurs après ces modifications.
Ensuite, typedef te défini un type pas une variable. Enlève le typedef (mais pas ce qu'il y a derrière) ça devrais aller mieux.
Pour finir a=b si a et b sont des tableaux NE recopie PAS le tableaux b dans a. Il peut le faire, mais ce n'est pas standard.
Dit nous si tu as encore des erreurs après ces modifications.
piece.c:11:8: error: incompatible types when assigning to type `char[4][4]' from type `char (*)[2]'
piece.c:12:2: error: `struct piece' has no member named `initializer'
piece.c:13:2: error: `struct piece' has no member named `afficher'
piece.c: In function `initializer':
piece.c:17:3: error: `piece' undeclared (first use in this function)
piece.c:17:3: note: each undeclared identifier is reported only once for each function it appears in
piece.c:17:9: error: expected `;' before `P'
piece.c: In function `afficher':
piece.c:23:1: error: `piece' undeclared (first use in this function)
piece.c:23:7: error: expected `;' before `P'
Remarque : initializer prend 3 paramètres, et tu l'utilises avec 4 ! Enlève le dernier paramètre.
Enfin, soit tu utilises ta variables globales struct piece P (sans le typedef comme le disait CharSnipeur) soit tu déclares des variables locales struct piece, mais tu ne peux pas déclarer "piece P", 1) car il te manque le mot clé struct, 2) car P est déjà déclaré globalement
Alors la notion P.hauteure_piece n'existe pas.Comment donc les initiliser?
alors je peux pas déclarer des fonctions dans le fichier struct et puis le définir dans piece.h
j'ai envoyé un autre question sur le forum qui contient mon dernier correction de code.
#include<stdio.h>
#include"piece.h"
int main()
{
piece P;int i,j;
for(i=0;i<2;i++)
P.forme[i][0]='@';
for(j=1;j<3;j++)
P.forme[0][j]='@';
initializer(3,2,P.forme,P);
afficher(P);
return 0;
}
void initializer(int n, int p, char tableau[4][4],piece P)
{
P.hauteur_piece=n;
P.largeure_piece=p;
int i,j ;
for( i=0;i<=4;i++)
for(j=0;j<=4;i++)
{
P.forme[i][j]=tableau[i][j];
}
}
void afficher(piece P)
{
int i,j;
for( i=0;i<=4;i++)
for(j=0;j<=4;i++)
{
printf("%c",P.forme[i][j]);
}
}
#include<stdio.h>
typedef struct
{
int hauteur_piece;
int largeure_piece;
char forme[4][4];
} piece;
struct piece P;
piece.c:15:6: warning: conflicting types for `initializer'
piece.c:10:1: note: previous implicit declaration of `initializer' was here
piece.c:29:6: warning: conflicting types for `afficher'
piece.c:11:1: note: previous implicit declaration of `afficher' was here
piece.h:8:16: error: storage size of `P' isn't known