1. Définir une structure
Une structure regroupe plusieurs champs sous un même type.
struct Point {
int x;
int y;
};
struct Point p = {3, 4};
printf("%d, %d\n", p.x, p.y);
Une structure regroupe plusieurs champs sous un même type.
struct Point {
int x;
int y;
};
struct Point p = {3, 4};
printf("%d, %d\n", p.x, p.y);
typedef crée un alias de type plus court.
typedef struct {
char nom[50];
int age;
} Personne;
Personne p = {"Alice", 25};
Avec un pointeur, on utilise -> au lieu de .
Créer une structure Etudiant (nom, note) et afficher un étudiant.
typedef struct {
char nom[50];
float note;
} Etudiant;
Etudiant e = {"Sami", 15.5f};
printf("%s : %.2f\n", e.nom, e.note);
Avec une struct Point, calculer la distance entre deux points.
#include <math.h>
float distance(struct Point a, struct Point b) {
int dx = a.x - b.x, dy = a.y - b.y;
return sqrt(dx*dx + dy*dy);
}