Skip to content
Snippets Groups Projects
Commit 7684dc2c authored by orestis.malaspin's avatar orestis.malaspin
Browse files

added advanced pointers

parent 3a1001bc
No related branches found
No related tags found
No related merge requests found
......@@ -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 $@ $<
......
......@@ -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
% 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment