Skip to content
Snippets Groups Projects
Commit ff138532 authored by thib's avatar thib
Browse files

answers

parent 9b4cfce2
No related branches found
No related tags found
No related merge requests found
<!-- EX2 -->
void foo(int a) {
a += 3;
}
<!-- a=5 -->
void bar(int *a) {
*a += 3;
}
<!-- a=8 -->
void baz(int *a) {
a += 3;
}
<!-- adresse de a +3 p.e erreur-->
int main() {
int a = 5;
foo(a);
bar(&a);
baz(&a);
}
<!-- EX 2 --> ex 1:
char *foo(int a) { 1. copie a reste 5
char tab[a]; 2. valeur de a + 3
<!-- manque un malloc sinon le return ne marchera pas (adresse invalide en dehors de la fonction) --> 3. adresse de a + 3 possible erreur
for (int i = 0; i < a; ++i) {
tab[i] = 'a';
}
return tab;
}
char *bar(int a) { ex 2:
char *tab = malloc(a); 1. manque un malloc sinon le return ne marchera pas (adresse invalide en dehors de la fonction)
for (int i = 0; i < a; ++i) { 2. corriger : malloc(sizeof(char)*a) a=[a,a,a,a]
tab[i] = 'a'; 3. malloc 1 fois en trop
}
return tab;
}
void baz(char *tab, int b) {
tab = malloc(b);
for (int i = 0; i < a; ++i) {
tab[i] = 'a';
}
}
int main() {
char *a = foo(4); <!-- a=[a,a,a,a] -->
char *b = bar(4);
char *c = malloc(4);
baz(c, 4);
free(a);
free(b);
free(c);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment