tomsawyerbelgique
Messages postés6Date d'inscriptionmardi 18 mars 2003StatutMembreDernière intervention29 mai 2003
-
30 avril 2003 à 21:54
CoreBreaker
Messages postés540Date d'inscriptiondimanche 23 mars 2003StatutMembreDerniè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.
CoreBreaker
Messages postés540Date d'inscriptiondimanche 23 mars 2003StatutMembreDernière intervention 5 octobre 20071 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);