Programme en c

Résolu/Fermé
sofi - Modifié par sofi le 15/09/2011 à 10:12
 sofi - 15 sept. 2011 à 18:08
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:
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'

1 réponse

Char Snipeur Messages postés 9696 Date d'inscription vendredi 23 avril 2004 Statut Contributeur Dernière intervention 3 octobre 2023 1 297
15 sept. 2011 à 11:39
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.
0
oui il y a :
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'
0
en faite comment je peux déclarer les fonctions d'une structure: dans le fichier .h ou bien non.
0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
15 sept. 2011 à 15:07
Tu utilises une notation pointée pour tes fonctions (P.initializer et P.afficher) qui n'a rien à faire dans un code C. Utilises directement initializer et afficher sans le "P." devant.
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
0
c'est tout à fait différent de C++. PAr contre je l'ai vue dans tel document sur c dans l'internet.
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.
0
Mon deuxiéme question a était supprimer. Je vous récopie le dernier code une autre fois dans j'aitrouvé des conflits
#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
0