Peut-être cet exemple pourrait aider:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>
struct aac {
char **pastries;
};
void
init_pastries (struct aac * rec_p, int max)
{
/* Allocate space for max #/pastries, plus a NULL delimiter */
int nbytes = sizeof (char *) * (max+1);
rec_p->pastries = (char **)malloc (nbytes);
memset (rec_p->pastries, 0, nbytes);
printf ("init_pastries: max #/pastries: %d, malloc: %d bytes...\n",
max, nbytes);
}
void
add_pastry (char * name, int i, struct aac *rec_p)
{
int nbytes = strlen (name) + 1;
rec_p->pastries[i] = (char *)malloc (nbytes);
strcpy (rec_p->pastries[i], name);
printf ("add_pastry (%s): malloc= %d bytes...\n",
name, nbytes);
}
void
print_pastries (struct aac * rec_p)
{
char **s = NULL;
for (s = rec_p->pastries; (*s); s++)
printf ("pastry: %s...\n", *s);
}
int
main () {
struct aac rec;
init_pastries (&rec, 5);
add_pastry ("cake", 0, &rec);
add_pastry ("donut", 1, &rec);
print_pastries (&rec);
return 0;
}
est ici un exemple de sortie:
gcc -o tmp tmp.c
./tmp
init_pastries: max #/pastries: 5, malloc: 24 bytes... add_pastry
(cake): malloc= 5 bytes... add_pastry
(donut): malloc= 6 bytes... pastry:
cake... pastry: donut...
« espoir qui aide ... au moins un peu :)
La version actuelle de votre exemple (# 4) fonctionne bien pour moi. L'avez-vous essayé après avoir ajouté 'char *' au paramètre 'value' de la fonction' cake() '? – MatthewD
Oui, j'ai fait une erreur en essayant de reconstruire le problème. – Jay
cette version de code ne fonctionne pas; prototypes manquants pour le gâteau et donut – user411313