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 54 55 56 57 58 59 60 61 62 63 64 65
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAB_MAX 5
#define STR_MAX 30
int main (void)
{
char ** tab = NULL;
int i = 0;
/*
* Allocation du tableau de pointeurs sur char.
*/
tab = malloc (TAB_MAX * sizeof (* tab));
if (tab != NULL)
{
/*
* Allocation des espaces pour les chaines du tableau.
*/
for (i = 0; i < TAB_MAX; i++)
{
tab [i] = malloc (STR_MAX * sizeof (** tab));
if (tab [i] != NULL)
{
strcpy (tab [i], "Une chaine de mon tableau !");
}
}
}
/*
* Lecture des chaines.
*/
i = 0;
for (i = 0; i < TAB_MAX; i++)
{
printf ("%s\n", tab [i]);
}
/*
* Liberation des chaines et du tableau.
*/
i = 0;
for (i = 0; i < TAB_MAX; i++)
{
if (tab [i] != NULL)
{
free (tab [i]);
tab [i] = NULL;
}
}
free (tab);
tab = NULL;
return EXIT_SUCCESS;
} |
Partager