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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct UneStructure {
size_t taille; /* Taille du tableau */
int tableau[1]; /* Tableau souple */
};
struct UneStructure *creer_structure(size_t taille_tableau)
{
struct UneStructure *pself = NULL;
size_t taille_struct = sizeof *pself + (taille_tableau - 1) * sizeof (int);
pself = malloc(taille_struct);
if (pself != NULL)
{
memset(pself, 0, taille_struct);
pself->taille = taille_tableau;
}
return pself;
}
int main(void)
{
struct UneStructure *s= creer_structure(10);
if (s != NULL)
{
/* traitements sur s */
free(s), s = NULL;
}
return EXIT_SUCCESS;
} |
Partager