chapitre 9

Les structures

 

1-    Le type structure :

 

1* introduction :

 

En enregistrement appelé structure on langage c est une variable qui permet d’imbriqué sous un seul nom plusieurs valeurs de types différents.

Chaque de ses éléments s’appelle champ.

Pour accédé à un champ if suffit d’utilisé le nom de la structure.

 

Pour déclaré une structure il faut définir un modèle ont indiquons les champs de la structure c'est-à-dire le type et le nom des variables qui la compose.

Syntaxe :

 

Struct nom_structure

{

Type_champ1 nom_champ1 ;

.

.

.

.

Type_champn nom_champn ;

} ;

 

Exemple :

 

Struct etudiant             

{

int age ;

float moyenne ;

Char nom[10] ;

} ;

 

è La déclaration de la structure ne fait que donnée l’allure.

C'est-à-dire une définitioàn d’un type de variable complexe.

 

è La déclaration ne reserve donc pas l’espace mémoire pour une variable structuré.il faut donc définir un ou plusieurs variables structuré après avoir déclaré la structure.

 

        2* définition d’une variable de type structure :

 

C’est une opération qui consiste à crée une variable aillons comme type celui d’une structure que l’ont déjà déclaré (nommée et réservé un emplacement dans la mémoire).

 

On le définie comme suit :

 

Struct nom_structure nom_variable ;

 

Exemple :

 

   Struct etudiant             

{

float moyenne ;

Char nom[10] ;

} ;

Struct etudiant E1 ; /*E1 est le nom du variable de type  struct etudiant*/    

 

On peut faire aussi ;

 

   Struct etudiant             

{

float moyenne ;

Char nom[10] ;

E1 ;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

 

è E1 désigne un étudiant.

 

Exemple 2 :

 

   Struct etudiant             

{

float moyenne ;

Char nom[10] ;

E1,E2 ;      

 

è Consiste à remplir des informations de deux étudiants (E1, E2).

 

L’ors de la déclaration on peut initialiser une structure ont indiquons entre accolade la liste des valeurs séparé par des virgules (chaque valeur à le type du son champ).

 

Exemple :

 

Struct etudiant E1={12, "mahfoudhi"} ;

 

Pour modifier une valeur d’un champ donnée il suffit d’accédé au champ d’une structure en utilisant l’opérateur du champ (un point) placer entre le nom de la variable structuré et le nom du champ.

 

Syntaxe :

 

nom_variable.nom_champ ;

 

Exemple :

 

E1.moyenne=14 ;          /*on efface 12 et on le remplace par 14*/

 

Pour faire le remplissage : (par l’utilisateur)

 

Scanf("%f",&E1.moyenne) ;

 

2-    Structure comportant des tableaux :

 

Exemple :

 

Struct etudiant

{

Pour me contacté : mahfoudhighaieth2007@yahoo.fr