1. Qu’est-ce qu’une structure ?

En C, une structure (struct) est un type de données qui permet de regrouper plusieurs variables (de types différents ou identiques) sous un même nom.

Idéal pour représenter des objets réels : un point, un étudiant, un complexe, etc.

2. Déclaration d’une structure

Syntaxe générale :

struct NomStructure {
    type1 membre1;
    type2 membre2;
    // ...
};

Exemple : un point 2D

struct Point {
    float x;
    float y;
};

Utilisation d’une structure

a. Déclarer une variable de type struct

struct Point p1;

b. Accéder aux membres

p1.x = 3.5;
p1.y = 2.0;

printf("x = %.1f, y = %.1f\n", p1.x, p1.y);

4. Alias avec typedef (optionnel mais pratique)

typedef struct {
    float x;
    float y;
} Point;

Point p1;

➡️ Plus besoin d’écrire struct Point, juste Point.

Exemple final : nombre complexe

Un nombre complexe a :

  • une partie réelle (re)
  • une partie imaginaire (im)

Déclaration :

typedef struct {
    float re;  // partie réelle
    float im;  // partie imaginaire
} Complexe;

Exemple complet avec addition de complexes

#include <stdio.h>

// Définition du type Complexe
typedef struct {
    float re;
    float im;
} Complexe;

// Fonction d'addition
Complexe addition(Complexe a, Complexe b) {
    Complexe res;
    res.re = a.re + b.re;
    res.im = a.im + b.im;
    return res;
}

// Affichage
void afficher(Complexe z) {
    printf("%.2f + %.2fi\n", z.re, z.im);
}

int main() {
    Complexe z1 = {3.0, 2.0};
    Complexe z2 = {1.5, -4.0};

    Complexe somme = addition(z1, z2);

    printf("z1 = "); afficher(z1);
    printf("z2 = "); afficher(z2);
    printf("z1 + z2 = "); afficher(somme);

    return 0;
}
z1 = 3.00 + 2.00i
z2 = 1.50 + -4.00i
z1 + z2 = 4.50 + -2.00i

Résumé

ÉlémentExemple
Déclarationstruct Complexe { float re, im; };
VariableComplexe z;
Accèsz.re, z.im
AvantageOrganisation claire des données

Encore un exemple , la multiplication de nombres complexes

Objectif :

  • Déclarer une structure Complexe
  • Implémenter :
    • une fonction afficher()
    • une fonction addition()
    • une fonction multiplication()
  • Afficher les résultats

🧠 Rappel mathématique

Si :
z1 = a + bi
z2 = c + di

Alors :
z1 × z2 = (ac – bd) + (ad + bc)i

Code complet avec multiplication :

#include <stdio.h>
#include <stdlib.h>

/* Définition d'un nombre complexe */
typedef struct {
    float re;
    float im;
} Complexe;

/* Affiche un complexe */
void afficher(Complexe z) 
{
    printf("%.2f + %.2fi\n", z.re, z.im);
}

/* Additionne deux complexes */
Complexe addition(Complexe a, Complexe b) 
{
    Complexe res;
    res.re = a.re + b.re;
    res.im = a.im + b.im;
    return res;
}

/* Multiplie deux complexes */
Complexe multiplication(Complexe a, Complexe b) 
{
    Complexe res;
    res.re = a.re * b.re - a.im * b.im;
    res.im = a.re * b.im + a.im * b.re;
    return res;
}

int main() 
{
    Complexe z1 = {2.0, 3.0};  /* z1 = 2 + 3i */
    Complexe z2 = {1.0, -4.0}; /* z2 = 1 - 4i */

    Complexe somme = addition(z1, z2);
    Complexe produit = multiplication(z1, z2);

    printf("z1 = "); afficher(z1);
    printf("z2 = "); afficher(z2);

    printf("z1 + z2 = "); afficher(somme);
    printf("z1 * z2 = "); afficher(produit);

    return EXIT_SUCCESS;
}
z1 = 2.00 + 3.00i
z2 = 1.00 + -4.00i
z1 + z2 = 3.00 + -1.00i
z1 * z2 = 14.00 + -5.00i