Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours_prog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
yassin.elhakoun
cours_prog
Commits
f0314e1c
Verified
Commit
f0314e1c
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
ajout slides tableaux/fonctions
parent
3ba5d9a1
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/tableaux_fonctions.md
+213
-0
213 additions, 0 deletions
slides/tableaux_fonctions.md
with
213 additions
and
0 deletions
slides/tableaux_fonctions.md
0 → 100644
+
213
−
0
View file @
f0314e1c
---
title
:
"
Tableaux
et
fonctions"
date
:
"
2021-10-05"
patat
:
wrap
:
true
margins
:
left
:
10
right
:
10
---
# Les tableaux statiques
## Qu'est-ce qu'un tableau statique?
. . .
*
Une
**liste**
ou un
**ensemble**
. . .
*
d'éléments du
**même type**
. . .
*
alloués de façon
**contigüe**
(en bloc) en mémoire
. . .
*
sur la
**pile**
. . .
*
dont la taille
**ne peut pas**
être changée.
. . .
## Remarques
-
Les éléments d’un tableau sont accédés avec
`[i]`
{.C} où
`i`
{.C} est l’index de l’élément.
-
Le premier élément du tableau à l’index
`0`
{.C}!
-
Lorsqu’un tableau est déclaré, la taille de celui-ci doit toujours être spécifiée, sauf s’il est initialisé lors de sa déclaration.
-
Un tableau local à une fonction ne doit
**jamais être retourné**
!
# Syntaxe des tableaux
```
C
float tab1[5]; // tableau de floats à 5 éléments
// ses valeurs sont indéfinies
int tab2[] = {1, 2, 3}; // tableau de 3 entiers,
// taille inférée
int val = tab2[1]; // val vaut 2 à présent
int w = tab1[5]; // index hors des limites du tableau
// comportement indéfini!
// pas d'erreur du compilateur
```
<!-- TODO QUIZ:
```
C
int a1[5]; // OK
int a2[] = { 1, 2, 3 }; // OK
int a3[4][5]; // OK
int [] a4; // Erreur
int a5[]; // Erreur
int[] function(void) { // Erreur
int array[5]; // OK
return array; // Erreur
}
void foo(int a[]) { // OK
a[3] = 0; // OK
}
void bar(void) {
int a[5]; // OK
foo(a); // OK
a = a[5]; // Erreur
}
```
-->
<!--
```C
#include <stdio.h>
int main(void) {
char i;
char a1[] = { 100,200,300,400,500 };
char a2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
a2[10] = 42;
for (i = 0; i < 5; i++) {
printf("a1[%d] = %d\n", i, a1[i]);
}
return 0;
}
```
-->
# Itérer sur les éléments d'un tableau
```
C
int x[10];
for (int i = 0; i < 10; ++i) {
x[i] = 0;
}
int j = 0;
while (j < 10) {
x[j] = 1;
j += 1;
}
int j = 0;
do {
x[j] = -1;
j += 1;
} while (j < 9)
```
# Représentation des tableaux en mémoire
## La mémoire est :
-
... contigüe,
-
... accessible très rapidement
## Exemple:
```
C
char tab[4] = {79, 91, 100, 88};
```
+------+------+------+------+------+------+------+
| char | 79 | 91 | 100 | 88 | .... | .... |
+======+======+======+======+======+======+======+
| addr | 2000 | 2001 | 2002 | 2003 | .... | .... |
+------+------+------+------+------+------+------+
## Qu'est-ce que `tab`?
```
C
tab; // 2000, l'adress du 1er élément
&tab[0]; // 2000 == tab
tab[0]; // 79
sizeof(tab); // 4
```
# Les tableaux comme arguments de fonctions
-
Un tableau en argument est le pointeur vers sa première case.
-
Pas moyen de connaître sa taille:
`sizeof()`
{.C} inutile.
-
Toujours spécifier la taille d'un tableau passé en argument.
```C
void foo(int tab[]) { // équivalent à int *tab
// que vaut sizeof(tab)?
for (int i = 0; i < ?; ++i) { // taille?
printf("tab[%d] = %d\n", i, tab[i]);
}
}
void bar(int n, int tab[n]) { // [n] optionnel
for (int i = 0; i < n; ++i) {
printf("tab[%d] = %d\n", i, tab[i]);
}
}
```
# Quels sont les bugs dans ce code?
```
C
#include <stdio.h>
int main(void) {
char i;
char a1[] = { 100,200,300,400,500 };
char a2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
a2[10] = 42;
for (i = 0; i < 5; i++) {
printf("a1[%d] = %d\n", i, a1[i]);
}
return 0;
}
```
# Quels sont les bugs dans ce code?
```
C
#include <stdio.h>
int main(void) {
char i;
// 200, .., 500 char overflow
char a1[] = { 100,200,300,400,500 };
char a2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
a2[10] = 42; // [10] out of bounds
for (i = 0; i < 5; i++) {
printf("a1[%d] = %d\n", i, a1[i]);
}
return 0;
}
```
<!-- TODO quiz: -->
<!-- que retourne sizeof(tab[]) -->
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment