1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include<stdio.h>
/* Il manque un fichier d'en tete pour malloc, realloc... */
#include <stdlib.h>
#include <string.h>
int main (void)
{
int taille = 7;
char *mot;
int i = 0;
/* Le cast est inutile voir deconseille */
mot = /*(char *)*/malloc (taille);
/* Un peu brutal
if (mot == NULL)
exit(1);*/
if (mot)
{
/* Non ce n'est pas comme cela que l'on copie une chaine dans une variable
mot="bonjou"; */
strcpy (mot, "bonjou");
while (mot[i] != '\0')
{
i++;
}
{
void *tmp = NULL;
/* realloc aussi peut echouer */
tmp = realloc (mot, taille + 1);
if (tmp)
{
mot = tmp;
mot[i] = 'n';
mot[i+1] = '\0';
printf ("%s", mot);
}
else
{
free (mot), mot = NULL;
fprintf (stderr, "Memoire insufisante");
return EXIT_FAILURE;
}
}
free (mot), mot = NULL;
}
else
{
fprintf (stderr, "Memoire insufisante");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} |