diff --git a/Makefile b/Makefile index b72a63de647a0507865863b4b6e4291482ab010c..bf98298db6cc381f3199aa967f2a6778b176a833 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 4332ee9bf581835b66169d5084bba2e3e98aa509..cc189ddf82406a2d2be68b054c176eaa822a6426 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 0000000000000000000000000000000000000000..61091039a92d2feebe89ccff77363d6ec56c17dd --- /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