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
| typedef struct CaractAnimal
{
int poids;
int taille;
int vitesse;
} CaractAnimal;
CaractAnimal proprietes( const char nom[] ) {
CaractAnimal animal;
static const CaractAnimal Loup = {80,80,60};
static const CaractAnimal Ours = {300,150,30};
static const CaractAnimal Lynx = {30,60,80};
if ( stricmp( nom , "Loup") == 0 ) {
animal = Loup; // une structure se copie avec le '='
}
else if ( stricmp( nom , "Ours" ) == 0 ) {
animal = Ours;
}
else if ( stricmp( nom , "Lynx" ) == 0 ) {
animal = Lynx;
}
else {
printf( "animal '%s' est inconnu\n" );
}
return animal; // une structure peut être retournée par une fonction
}
int main() {
char nomAnimal[5] = "Loup";
CaractAnimal caractAnimal = proprietes( nomAnimal );
printf( "\nVos caracteristiques :\n\n" );
printf( " Animal : %s\n", nomAnimal ); // on utilise %s pour des chaînes
printf( " Poids : %d\n", caractAnimal.poids ); // on utilise %d pour les entiers
printf( " Taille : %d\n", caractAnimal.taille );
printf( " Vitesse: %d\n", caractAnimal.vitesse );
return 0;
} |
Partager