From 7684dc2c4df764b8244c93bf827e624bf6d45a2e Mon Sep 17 00:00:00 2001
From: Orestis Malaspinas <orestis.malaspinas@hesge.ch>
Date: Fri, 14 Feb 2020 15:57:27 +0100
Subject: [PATCH] added advanced pointers

---
 Makefile             |  5 ++-
 index.md             |  7 +++-
 pointeurs_avances.md | 79 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 89 insertions(+), 2 deletions(-)
 create mode 100644 pointeurs_avances.md

diff --git a/Makefile b/Makefile
index b72a63d..bf98298 100644
--- a/Makefile
+++ b/Makefile
@@ -19,7 +19,7 @@ HTMLOPTIONS += -t html5
 HTMLOPTIONS += -c css/tufte-css/tufte.css
 HTMLOPTIONS += --self-contained
 
-all: oral.pdf ligne_commande.pdf strings.pdf tests_assertions.pdf make.pdf base_3.pdf base_2.pdf base_1.pdf intro.pdf index.html
+all: pointeurs_avances.pdf oral.pdf ligne_commande.pdf strings.pdf tests_assertions.pdf make.pdf base_3.pdf base_2.pdf base_1.pdf intro.pdf index.html
 
 intro.pdf: intro.md metadata.yaml
 	pandoc $(PDFOPTIONS) -o $@ $^
@@ -48,6 +48,9 @@ ligne_commande.pdf: ligne_commande.md metadata.yaml
 oral.pdf: oral.md metadata.yaml
 	pandoc $(PDFOPTIONS) -o $@ $^
 
+pointeurs_avances.pdf: pointeurs_avances.md metadata.yaml
+	pandoc $(PDFOPTIONS) -o $@ $^
+
 index.html: index.md
 	pandoc -s $(OPTIONS) $(HTMLOPTIONS) -o $@ $<
 
diff --git a/index.md b/index.md
index 4332ee9..cc189dd 100644
--- a/index.md
+++ b/index.md
@@ -70,4 +70,9 @@ corrige: false
 ## Ligne de commande [PDF](ligne_commande.pdf)
 
 - Point d’entrée d’un programme.
-- Conversion des arguments.
\ No newline at end of file
+- Conversion des arguments.
+
+## Pointeurs avancés [PDF](pointeurs_fonctions.pdf)
+
+- Pointeurs de fonctions.
+- Pointeurs et `const`.
\ No newline at end of file
diff --git a/pointeurs_avances.md b/pointeurs_avances.md
new file mode 100644
index 0000000..6109103
--- /dev/null
+++ b/pointeurs_avances.md
@@ -0,0 +1,79 @@
+% Pointeurs avancés
+% Inspirés des slides de F. Glück
+% 19 février 2020
+
+# Pointeurs avancés
+
+## Pointeurs de fonctions (1/2)
+
+- COnsidérons la fonction `max` retournant la valeur maximale d'un tableau
+
+    ```C
+    int max(int *t, int size) {
+        int val_max = t[0];
+        for (int i = 1; i < n; ++i) {
+            if (t[i] > val_max) {
+                val_max = t[i];
+            }
+        }
+        return max_val;
+    }
+    ```
+- L'appel à `max`, retourne l'adresse de la fonction en mémoire.
+- On peut affecter cette valeur à un pointeur.
+
+## Pointeurs de fonctions (2/2)
+
+- Le type de la fonction `max` est
+
+    ```C
+    int (*pmax)(int *, int);
+    ```
+- Le type doit être déclaré avec la signature de la fonction.
+- On peut alors utiliser l'un ou l'autre indiféremment
+
+    ```C
+    int (*pmax)(int *, int);
+    pmax = max;
+    int tab[] = {1, 4, -2, 12};
+    printf("%d", max(tab, 4)); // retourne 12
+    printf("%d", pmax(tab, 4)); // retourne 12 aussi
+    ```
+
+# Pointeurs et `const`
+
+## Deux niveaux de constance
+
+- Le mot clé `const` permet de déclarer des valeurs "constantes" qui ne changeront plus en cours d'exécution du programme.
+- Mais qu'est-ce que cela veut dire pour les pointeurs?
+
+    ```C
+    int n = 12;
+
+    const int *p = &n; // la valeur *p est const, p non
+    int const *p = &n; // la valeur *p est const, p non
+    int *const p = &n; // la valeur p est const, *p non
+    const int *const p = &n; // la valeur p et *p sont const
+    ```
+
+## Exemples
+
+```C
+int n = 12; int m = 13;
+
+const int *p = &n; // la valeur *p est const, p non
+*p = m; // erreur de compilation.
+p = &m; // OK
+
+int const *p = &n; // la valeur *p est const, p non
+*p = m; // erreur de compilation.
+p = &m; // OK
+
+int *const p = &n; // la valeur p est const, *p non
+*p = m; // OK
+p = &m; // erreur de compilation.
+
+const int *const p = &n; // la valeur p et *p sont const
+*p = m; // erreur de compilation.
+p = &m; // erreur de compilation.
+```
\ No newline at end of file
-- 
GitLab