[VOID*] Implémentation stockage de structures différentes
Bonjour,
Je souhaite créer une structure contenant deux élements
Code:
1 2 3 4 5
|
typedef struct _cont {
int nbElement;
void *tab;
} Container; |
afin de stocker toute sorte d'autres structures (du même type par instance de Container).
Pour cela j'aimerais pouvoir utiliser la même fonction d'ajout pour n'importe quelle structure à ajouter.
Code:
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
|
int putInContainer(Container *container, void *element) {
if (!container || !element) {
return KO;
}
/* allocation (je suis vraiment pas sûr pour le sizeof...) */
void *tmp = realloc(container->tab, sizeof(element) * (container->nbLine + 1));
if (tmp) {
container->tab = tmp;
} else {
/* erreur allocation mémoire */
return KO;
}
/* affectation qui fonctionne pas vraiment */
container->tab[container->nbLine] = element;
container->nbLine++;
return OK;
}
int main() {
Container *co = malloc(sizeof(Container));
co->nbLine = 0;
co->tab = NULL;
if (putInContainer(co, pointeur_sur_structure) {
printf("pb\n");
exit(1);
}
(...) |
Bon déjà ce code ne compile pas en utilisant une véritable structure à stocker :oops:
J'ai un problème pour l'allocation, en principe sizeof prend en paramètre un type de donnée il me semble,
et un problème pour l'affectation, je ne sais pas trop comment m'y prendre
Merci d'avance
Re: [VOID*] Implémentation stockage de structures différente
Citation:
Envoyé par tomasha
Je souhaite créer une structure contenant deux élements <...>
Bon, et à part me réveiller pendant ma sieste, on est censé faire quoi avec ça ?
Code:
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
|
Compiling: main.c
main.c:6: warning: no previous prototype for 'putInContainer'
main.c: In function `putInContainer':
main.c:8: error: `KO' undeclared (first use in this function)
main.c:8: error: (Each undeclared identifier is reported only once
main.c:8: error: for each function it appears in.)
main.c:11: error: implicit declaration of function `realloc'
main.c:11: warning: nested extern declaration of `realloc'
main.c:11: error: structure has no member named `nbLine'
main.c:11: warning: initialization makes pointer from integer without a cast
main.c:19: error: structure has no member named `nbLine'
main.c:20: error: structure has no member named `nbLine'
main.c:21: error: `OK' undeclared (first use in this function)
main.c: In function `main_':
main.c:25: error: implicit declaration of function `malloc'
main.c:25: warning: nested extern declaration of `malloc'
<internal>:0: warning: redundant redeclaration of 'malloc'
main.c:26: error: structure has no member named `nbLine'
main.c:27: error: `NULL' undeclared (first use in this function)
main.c:29: error: `pointeur_sur_structure' undeclared (first use in this function)
main.c:29: error: syntax error before '{' token
main.c:31: error: implicit declaration of function `exit'
main.c:31: warning: nested extern declaration of `exit'
<internal>:0: warning: redundant redeclaration of 'exit'
main.c: At top level:
main.c:34: error: syntax error before '}' token
main.c:34:2: warning: no newline at end of file
Process terminated with status 1 (0 minutes, 0 seconds) |
Re: [VOID*] Implémentation stockage de structures différente
Citation:
Envoyé par Emmanuel Delahaye
Citation:
Envoyé par tomasha
Je souhaite créer une structure contenant deux élements <...>
Bon, et à part me réveiller pendant ma sieste, on est censé faire quoi avec ça ?
:pan: