Structure pointant sur un tableau de structures

Résolu
timy94 Messages postés 50 Date d'inscription jeudi 17 novembre 2005 Statut Membre Dernière intervention 7 janvier 2009 - 2 juin 2007 à 16:51
The_Guardian Messages postés 317 Date d'inscription vendredi 25 mai 2007 Statut Membre Dernière intervention 19 octobre 2007 - 2 juin 2007 à 18:23
Bonjour
Voila mon problème. En C j'ai une structure ayant comme élément un pointeur sur un tableau de structures. Comment faire le malloc et comment accéder au éléments.

Exemple:
struct ST_Toto {
    int variable
 };

struct My_Struct_Tab {
    char chaine [10];
    struct ST_Toto * tab;
} Mon_Tableau[100];

En cours de programme je voudrais dimensionner Mon_Tableau, de 100 éléments, pour que chaque élément contienne un tableau de structure de 20 ST_Toto.

Mon_Tableau = (struct *) malloc ?????

Comment accéder à un élément variable. Et comment procéder en C++, voir JAVA.

MERCI à tous
Timy94</gras>

1 réponse

The_Guardian Messages postés 317 Date d'inscription vendredi 25 mai 2007 Statut Membre Dernière intervention 19 octobre 2007 1
2 juin 2007 à 18:23
Salut

 bien alors deja modifie un peu ta declaration pour avoir
struct My_Struct_Tab { ... };
 donc pas de Mon_Tableau[100] pour le moment
puis tu fais struct My_Struct_Tab * monTableau;
monTableau = (struct My_Struct_Tab *)malloc(100*sizeof(struct My_Struct_Tab));
for (i=0; i<100; i++) {
 monTableau[i].tab = (struct ST_Toto *)malloc(20*sizeof(struct ST_Toto));
 }
Et au final tu pourras faire :
monTableau[50].tab[10].variable;

=

Une autruche ne se cuit pas aux petits lardons
3
Rejoignez-nous