08.04.2014 Views

Algorithmique et Langage - Pages de Michel Deloizy - Free

Algorithmique et Langage - Pages de Michel Deloizy - Free

Algorithmique et Langage - Pages de Michel Deloizy - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Exemple<br />

pgm.exe –x3<br />

toto.dat titi.out<br />

• argc = 4<br />

• argv[0]="<br />

[0]="pgm.exe"" (avec le chemin d'accès compl<strong>et</strong>)<br />

• argv[1]="<br />

[1]="-x3"<br />

• argv[2]="<br />

[2]="toto.dat"<br />

• argv[3]="<br />

[3]="titi.out"<br />

M. <strong>Deloizy</strong> <strong>Langage</strong> C<br />

133<br />

Pointeurs <strong>de</strong> fonctions<br />

Nom <strong>de</strong> fonction → adresse <strong>de</strong> la fonction<br />

→ Pointeur <strong>de</strong> fonction<br />

Peut être utilisé pour in<strong>de</strong>xer une fonction parmi plusieurs.<br />

Exemple :<br />

void TriBulle(int<br />

int t[ ], int nb);<br />

void TriInsertion(int<br />

int t[ ], int nb);<br />

void (*fctTri<br />

fctTri)( )(int<br />

t[ ], int nb);<br />

…<br />

int tab[ ] = {1, 4, 0, -1, 2};<br />

fctTri = TriBulle;<br />

fctTri(tab,<br />

(tab,sizeof(tab)/sizeof(*tab));<br />

fctTri = TriInsertion;<br />

fctTri(tab,<br />

(tab,sizeof(tab)/sizeof(*tab));<br />

M. <strong>Deloizy</strong> <strong>Langage</strong> C<br />

134<br />

Allocation dynamique <strong>de</strong> mémoire<br />

→ Quand un espace mémoire m moire est requis, <strong>de</strong> taille<br />

inconnue lors <strong>de</strong> la conception du programme<br />

→ Pour optimiser l'utilisation <strong>de</strong> la mémoirem<br />

moire<br />

→ Deman<strong>de</strong> <strong>de</strong> mémoire m moire à l'environnement<br />

Fonctions :<br />

• void * malloc(size_t<br />

sz);<br />

Renvoie un pointeur sur la zone allouée<br />

ou NULL si pas d'espace disponible<br />

• void free(void<br />

* ptr);<br />

Libère l'espace mémoire m moire attribué par malloc<br />

M. <strong>Deloizy</strong> <strong>Langage</strong> C<br />

135<br />

Exemple<br />

int nb, , *tab;<br />

printf("Nombre <strong>de</strong> valeurs à acquérir :");<br />

scanf("%d",&<br />

("%d",&nb);<br />

tab = malloc(nb<br />

nb*sizeof(*tab));<br />

if(tab!=NULL) {<br />

int i;<br />

for(i=0; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!