Tableau de structure

tomsawyerbelgique Messages postés 6 Date d'inscription mardi 18 mars 2003 Statut Membre Dernière intervention 29 mai 2003 - 30 avril 2003 à 21:54
CoreBreaker Messages postés 540 Date d'inscription dimanche 23 mars 2003 Statut Membre Dernière intervention 5 octobre 2007 - 2 mai 2003 à 03:45
Bonjour suriez-vous me dire comment fait-on pour passer un tableau de structure dans une fonction:

struct student {
char nom[10];
int nbcote;
int *ptcote;
}

j'ai un tableau de 10 struct student

strcut student tab[10];

Je voudrai bien pas une fonction, allouer dynamiquement de la mémoire pour un tableau de 5 cotes

student.ptcote=calloc(5,sizeof(int));

Mais j'aimerais faire cela dans une fonction donc comment passer le tableau de structure a la fonction et comment allouer la place mémoire.

Merci a ceux qui peuvent m'aider.

Tom Sawyer

2 réponses

cmarsc Messages postés 455 Date d'inscription mercredi 6 mars 2002 Statut Membre Dernière intervention 18 décembre 2003
1 mai 2003 à 10:34
salut,

il y a des codes en ligne qui peuvent répondre à tes questions
http://www.cppfrance.com/article.aspx?ID=653
http://www.cppfrance.com/listeauteur2.aspx?Val=137
0
CoreBreaker Messages postés 540 Date d'inscription dimanche 23 mars 2003 Statut Membre Dernière intervention 5 octobre 2007 1
2 mai 2003 à 03:45
C'est simple:

void alloc(struct student *ptab)
{
ptab->ptcote= calloc(5,sizeof(int));
}

ensuite:
alloc(&tab[3]);
ou
alloc(tab + 3);

ou si tu veux initialiser tout le tableau par la fonction:
void allocTab(struct student *ptab)
{
int i;
for(i= 0; i < 10; i++)
ptab[i].ptcote=calloc(5,sizeof(int));
}

ensuite:
allocTab(tab);

tu peux faire aussi:
void tabAlloc(struct student **ptab)
{
(*ptab)->ptcote=calloc(5,sizeof(int));
}

ensuite:
tabAlloc(&tab);


Core Breaker :)
0
Rejoignez-nous