0% ont trouvé ce document utile (0 vote)
16 vues14 pages

Structures en Langage C: Guide Complet

Le document présente les structures en langage C, permettant de créer des types de variables personnalisés avec des champs de différents types. Il explique la déclaration, l'initialisation, l'accès aux composants, ainsi que l'utilisation de typedef pour simplifier la déclaration de types. Des exemples pratiques illustrent l'affectation, la comparaison et les structures imbriquées.

Transféré par

mehdi benmassoud
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
16 vues14 pages

Structures en Langage C: Guide Complet

Le document présente les structures en langage C, permettant de créer des types de variables personnalisés avec des champs de différents types. Il explique la déclaration, l'initialisation, l'accès aux composants, ainsi que l'utilisation de typedef pour simplifier la déclaration de types. Des exemples pratiques illustrent l'affectation, la comparaison et les structures imbriquées.

Transféré par

mehdi benmassoud
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd

Année universitaire:2024/2025

Filière: ELT

Langage de Programmation: I
- Le Langage C –
Tableaux

Pr: [Link]
E-mail: ahmedchater11@[Link]
Les structures

Définition : le langage C nous permet de faire quelque chose de très puissant: il


nous permet de créer nos des types de variables personnalisés
Les structures
Une structure est un assemblage de variables qui peuvent avoir différents types
Contrairement aux tableaux qui vous obligent à utiliser le même type dans tout
le tableau, vous pouvez créer une structure comportant des variables de type
long ,char,int double à la fois
Déclaration
Struct nom_structure
{ type1 nomchamps2;
type2 nomchamps2;
.
typeN nomchamps2;};
Les structures

Exemple

Struct point
{
Int x;
Int y;
};
Void main()
{
Struct point A,B; //déclaration et l’initilisation
Struct point c={8,4};
};
Les structures

Exemple:
struct personne
{
char nom[100];
int age ;
float salaire;
};
Void main()
{
struct personne p1;
strcut personne P2={‘’AHMED ’’,26, 9000.0};
}
Les structures
L’utilisation de « typedef »
Typedef est un mot clé réservé dans le langage de programmation C. il est
utilisé pour créer nom d’alias (un nouveau nom) pour un autre type de
données.
Syntaxe typedef type_de_données alias;
Exemple: typedef int entier ;
Maintenant pour déclarer une variable entière on peut utiliser entier à la
place de int. struct point
{
int x;
Void main() int y;
{ };
typedef int entier ; Typedef struct point point ;
entier a=5; Void main()
entier b=5; {
entier T[10]; point A;
} }
Les structures
Accès aux composantes de la structure

Struct point
{
int x;
int y;
};
Typedef struct point point ;
Void main ()
{
Point A,B;

Printf(‘’ donner les coordonnées de A:’’);


Scanf(‘’%d%d’’,& A.x,& A.y );
Printf(‘’ donner les coordonnées de B:’’);
Scanf(‘’%d%d’’,& B.x,& B.y );
}
Autre exemple
Struct
Struct personne
{
Char nom[100];
Int age ;
Float salaire;
};
Typedef struct personne person;
Void main()
{
Person directeur;
Printf(‘’ donner son nom :’’);
Gets([Link]);
Printf(‘’donner son age: ‘’);
Scanf(‘’¨%d’’,&[Link]);
Printf(‘’donner son salaire: ‘’);
Scanf(‘’¨%f’’,&[Link]);
}
Les structures

L’affectation
On peut affecter une variable structure à une autre variable
structure de même type , grâce à l’opérateur d’affectation « =« :
( On n’a pas besoin d’affecter champs par champs)
Struct personne P1,P2
𝑠𝑡𝑟𝑐𝑝𝑦(𝑃1. 𝑛𝑜𝑚, 𝑃2. 𝑛𝑜𝑚;
P1=P2 ; ↔ 𝑃1. 𝑎𝑔𝑒 = 𝑃2. 𝑎𝑔𝑒;
𝑃1. 𝑠𝑎𝑙𝑎𝑖𝑟𝑒 = 𝑃2. 𝑠𝑎𝑙𝑎𝑖𝑟𝑒;
La comparaison
Aucune comparaison n’est possible sur les structures, même pas
les opérateurs == et != .
Exemple : Livre

-Titre
-Auteur
-Edition
-Prix
Les structures
Struct personne
{ Si on a à traiter plusieurs personnes on peut donc déclarer
char nom[100]; un tableau de personne.
Int age ;
Float salaire;
};
Typdef struct personne personne

Void main()
{personne equipe[50];
Int i;
For(i=0;i<22;i++)
{printf(‘’donner son nom:’’);
Gets(equipe[i].nom);
printf(‘’donner son age:’’);
scanf(‘’%d’’,&equipe[i].age);

printf(‘’donner son salaire :’’);


scanf(‘’%f’’,&equipe[i].salaire);}
}
Les structures imbriques
Les champs d’une structures peuvent être de tout type , y compris de type structuré
Struct Tdate { Typedef struct Tdate{
Int jour; Int jour;
Int mois; Int mois ;
Int annee}; Int annee;
Typedef struct Tdate Tdate; }Tdate;

Typedef struct personne


{ char nom[20];
Char pernom [20];
𝑗𝑜𝑢𝑟
Tdate naissance; 𝑚𝑜𝑖𝑠
𝑎𝑛𝑛𝑒𝑒
}personne;
L’initialisation
Personne prof ={‘’JAMALI’’,’’Ahmed’’, {19,10,1988}};
Accès aux champs d’une structure imbriquée

Personne prof ={‘’JAMALI’’,’’Ahmed’’, {19,10,1988}};


[Link]
𝑛𝑜𝑚 ′′𝐽𝐴𝑀𝐴𝐿𝐼′′
Prof 𝑝𝑟𝑒𝑛𝑜𝑚 ′′𝐴ℎ𝑚𝑒𝑑′′ [Link]
𝑛𝑎𝑖𝑠𝑠𝑎𝑛𝑐𝑒 .
𝐽𝑜𝑢𝑟 18 [Link]
𝑚𝑜𝑖𝑠 10
𝑎𝑛𝑛𝑒𝑒 1988
[Link]

[Link]

Common questions

Alimenté par l’IA

Structures are beneficial when dealing with entities that naturally encapsulate multiple related data items of various types, such as database records or complex data models like a 'person' with 'name', 'age', and 'salary'. They help in maintaining organized code by grouping related data together, minimizing the risk of errors and improving code readability .

Structures in C offer the advantage of holding multiple variables of different types, allowing for more complex data representations compared to arrays, which restrict all elements to a single type . This versatility is useful in representing complex data entities such as a 'person' with fields like name, age, and salary, each of potentially different data types, within one structure .

Input for structure fields is handled using standard input functions. For instance, 'scanf' can be used for integers and floats, and 'gets' for character arrays. Fields can be initialized directly with values at the point of declaration, such as 'struct point c={8,4};' or for nested structures like 'personne prof = {"JAMALI", "Ahmed", {19, 10, 1988}};' to set all fields in one action .

Structures organize data by enabling the encapsulation of different data types within a single entity, streamlining data management in complex applications. For example, a 'person' structure can include fields for 'name', 'age', and 'salary', each with its suitable data type, allowing for easy modeling and manipulation of data for various tasks like payroll systems or databases, enhancing both functionality and readability .

Using 'typedef' for structures simplifies code syntax and enhances readability by allowing the avoidance of the 'struct' keyword in variable declarations. This not only reduces typographical errors but also makes code more concise and easier to maintain, especially in large and complex programs . In contrast, using 'struct' directly requires repetitive typing and can introduce clutter in declarations and assignments.

In C, data from one structure variable can be assigned to another of the same type using the assignment operator '=', which allows for copying all fields at once without field-by-field assignment. However, direct comparison of structure variables using operators like '==' or '!=' is not supported, requiring manual field-by-field comparison for equality checks .

A notable limitation is the inability to directly compare entire structure variables using operators like '==' or '!='. This restriction demands field-by-field comparison or the use of workarounds like writing custom comparison functions. Structures also do not support built-in operations for arithmetic or sorting, which further complicates data manipulation .

Structures in C can be nested by including a structure as a member within another structure. To access nested fields, one must navigate through each level of the structure using dot notation. For example, given 'struct personne' with a 'Tdate naissance' field, the day of birth can be accessed with 'prof.naissance.jour' .

Typedef in C allows the creation of type aliases, which can increase code readability and simplify structure usage. By assigning a new name to a structure, such as replacing 'struct personne' with 'person' using 'typedef struct personne person;', programmers can declare variables of that type more succinctly, improving code clarity and reducing verbosity .

C allows different data types to coexist within a structure by defining each member independently, with its own specific data type. This feature negates the constraint of uniform data type required by arrays, enabling flexible data modeling within structures .

Vous aimerez peut-être aussi