ESTIA - Unit´ e d’Enseignement Math´ ematiques-Informatique 1
reAnn´ ee
·2019/2020
Programmation Proc´ edurale en Langage C – TD3
Pointeurs : port´ ee des variables, fonctions, tableaux et chaˆınes de caract` eres
Exercice 1 : Port´ ee des variables (tordons le cou ` a quelques id´ ees re¸ cues. . .)
1) Les trois programmes suivants peuvent-ils compiler sans erreur ? Lequel des trois fonctionne correctement ? Que font ces programmes ? Donnez les affichages que produiront leurs ex´ ecutions.
/* V E R S I O N 1 */
# i n c l u d e < s t d i o . h >
v o i d a j o u t e r ( int n ) { n += 100 ;
p r i n t f ( " a j o u t e r () : n v a u t % d \ n " , n ) ; }
int m a i n () { int i ; i = 64 ;
p r i n t f ( " m a i n () : i v a u t % d ( A V A N T ) \ n " , i ) ; a j o u t e r ( i ) ;
p r i n t f ( " m a i n () : i v a u t % d ( A P R E S ) \ n " , i ) ; r e t u r n 0 ;
}
/* V E R S I O N 2 */
# i n c l u d e < s t d i o . h >
v o i d a j o u t e r ( int i ) { i += 100 ;
p r i n t f ( " a j o u t e r () : i v a u t % d \ n " , i ) ; }
int m a i n () { int i ; i = 64 ;
p r i n t f ( " m a i n () : i v a u t % d ( A V A N T ) \ n " , i ) ; a j o u t e r ( i ) ;
p r i n t f ( " m a i n () : i v a u t % d ( A P R E S ) \ n " , i ) ; r e t u r n 0 ;
}
/* V E R S I O N 3 */
# i n c l u d e < s t d i o . h >
v o i d a j o u t e r ( int n ) { n += 100 ;
p r i n t f ( " a j o u t e r () : n v a u t % d \ n " , n ) ; }
int m a i n () { a j o u t e r ( 6 4 ) ; r e t u r n 0 ; }
(a) (d)
(b) (e)
(c) (f)
2) Identifiez les variables : globales, locales et statiques.
3) Apportez les modifications n´ ecessaires au bon fonctionnement de la fonction ajouter(). Comment devra-t- elle ˆ etre appel´ ee ? Que devra-t-elle prendre en argument ?
Exercice 2 : Manipulation de pointeurs 1) Que contient i ` a la ligne 6 ?
Que contient p ` a la ligne 8 ?
2) Que vaut i ` a la ligne 7 ? et ` a la ligne 10 ? Quels affichages produisent ces lignes ? 3) Expliquez ce qu’il se passe ` a la ligne 9.
1 # i n c l u d e < s t d i o . h >
2
3 int m a i n () {
4 int i , * p ;
5
6 i = 10 ;
7 p r i n t f ( " Le c o n t e n u de i v a u t % d \ n " , i ) ; 8 p = & i ;
9 * p += 10 ;
10 p r i n t f ( " Le c o n t e n u de i v a u t % d \ n " , i ) ; 11
12 r e t u r n 0 ; 13 }
Guillaume Rivi`ere 2010–2020
– 1 – L
ATEX
ESTIA - Unit´ e d’Enseignement Math´ ematiques-Informatique 1
reAnn´ ee
·2019/2020
Exercice 3 : Tableaux
1 # i n c l u d e < s t d i o . h >
2 # i n c l u d e < s t d l i b . h >
3
4 int m a i n () {
5 int * tab1 , * tab2 , N , i ;
6
7 /* R e c u p e r e r la t a i l l e du t a b l e a u */
8 p r i n t f ( " C o m b i e n de v a l e u r s : " ) ; 9 s c a n f ( " % d " , & N ) ;
10
11 /* R e s e r v e r un e s p a c e m e m o i r e */
12 t a b 1 = m a l l o c ( N * s i z e o f ( int ) ) ; 13 if ( t a b 1 == N U L L ) {
14 f p r i n t f ( stderr , " E r r o r : m e m o r y a l l o c a t i o n \ n " ) ;
15 e x i t (1) ;
16 }
17
18 /* I n i t i a l i s e r le t a b l e a u */
19 for ( i =0 ; i < N ; i ++)
20 t a b 1 [ i ] = 3 * i + 2 ;
21
22 /* C o p i e r le t a b l e a u */
23 t a b 2 = t a b 1 ;
24
25 /* D o u b l e r l e s v a l e u r s */
26 for ( i =0 ; i < N ; i ++) 27 t a b 2 [ i ] = t a b 2 [ i ] * 2 ; 28
29 /* A f f i c h e r le p r e m i e r t a b l e a u */
30 p r i n t f ( " t a b 1 = " ) ; 31 for ( i =0 ; i < N ; i ++)
32 p r i n t f ( " %2 d " , t a b 1 [ i ]) ; 33 p r i n t f ( " \ n " ) ;
34
35 /* A f f i c h e r le d e u x i e m e t a b l e a u */
36 p r i n t f ( " t a b 2 = " ) ; 37 for ( i =0 ; i < N ; i ++)
38 p r i n t f ( " %2 d " , t a b 2 [ i ]) ; 39 p r i n t f ( " \ n " ) ;
40
41 /* L i b e r e r la m e m o i r e a l l o u e e */
42 f r e e ( t a b 1 ) ; 43
44 r e t u r n 0 ; 45 }
1) Que fait le programme ? Quel est affichage produit-il ?
(a) (b) (c)
2) Que provoque l’instruction ligne 23 ? Que se passe-t-il en m´ emoire ?
3) Apportez les corrections n´ ecessaires pour obtenir le fonctionnement correct.
Exercice 4 : Chaˆınes de caract` eres
1 # i n c l u d e < s t d i o . h >
2 # i n c l u d e < c t y p e . h >
3 # i n c l u d e < s t r i n g . h >
4
5 int m a i n () {
6 c h a r c h a i n e [ 2 5 6 ] ; 7 c h a r * c h a i n e _ m a j ;
8 int i ;
9
10 /* I n i t i a l i s e r la c h a i n e de c a r a c t e r e */
11 s t r c p y ( chaine , " Hey \" d u d e \"! W h a t ’ s up ? " ) ; 12
13 /* C o p i e r la c h a i n e de c a r a c t e r e */
14 c h a i n e _ m a j = c h a i n e ; 15
16 /* M e t t r e en m a j u s c u l e */
17 for ( i =0 ; i < s t r l e n ( c h a i n e _ m a j ) ; i ++) 18 c h a i n e _ m a j [ i ] = t o u p p e r ( c h a i n e _ m a j [ i ]) ; 19
20 /* A f f i c h e r */
21 p r i n t f ( " c h a i n e = % s \ n " , c h a i n e ) ;
22 p r i n t f ( " c h a i n e _ m a j = % s \ n " , c h a i n e _ m a j ) ; 23
24 r e t u r n 0 ; 25 }
1) Que fait le programme ? Quel affichage produit-il ?
(a)
(b)
(c)
2) Que fait la fonction strcpy() ligne 11 ? Que calcule la fonction strlen() ligne 17 ? 3) Que provoque l’instruction ligne 14 ? Que se passe-t-il en m´ emoire ?
4) Comment obtenir le fonctionnement correct ? Quelle fonction de <string.h> utiliser ? Quelle fonction faut-il alors appeler en fin de programme ?
Guillaume Rivi`ere 2010–2020