So I'm working on a little C program which is a little address book that automatically allocates memory when you add a new contact in it.
I'm using two typedef structures, the first one stores the info on the contact (name telephone etc.):
typedef struct
{
char nom[TAILLE1];
char tel[TAILLE2];
} CONTACT;
The seconde one contains a int with the number of contacts in the address book and an other one is the pointer to the other structure.
typedef struct
{
int nb;
CONTACT * contacts; // tableau
} LISTE_TABLEAU;
I created a function to import contact from a TXT file (first line the name of the person, second line their telephone number and so on). I simplified it with only the basic
int lireDonneesTxt(LISTE_TABLEAU* tab)
{
int i;
tab->contacts = (CONTACT *)malloc(sizeof(CONTACT)*13); (13 because there are 13 contact for testing purposes)
i = 0;
while( !feof(entree) )
{
fgets(ligne, TAILLE1, entree);
strcpy(tab->contacts[i].nom, ligne);
fgets(ligne, TAILLE1, entree);
strcpy(tab->contacts[i].tel, ligne);
i++
}
return 1;
}
When I compile my code there isn't any issue, no warning what so ever.
But when I run my code everything works great until I try and print a name on the screen, then the executable file crashes.
My main function looks something like this:
int main(void)
{
LISTE_TABLEAU *tabb;
tabb->nb = 0;
lireDonneesTxt(&tabb);
printf("%s", tabb->contacts[0].nom);
return 0;
}
If I add the same printf that is in the main
at the end of my lireDonneesTxt
function it prints the name without any problem.
I'm guessing that the data is not passed correctly to the structure.
I am now blocked and have no idea what to try to make this work !