I'd like to extend my array containing a struct 'Article' using realloc, so I tell realloc to take the old array and extend it then I add my new Article to the new array but this doesn't work and i can't find the problem. this is the error message >corrupted size vs. prev_size Abandon (core dumped)
typedef struct
{
char ref[6];
float pu;
int qt;
char desi[31];
}Article;
void global(void)
{
FILE * fe;
Article *tart,l;
int nbart;
fe=fopen("a.txt","a+");
tart=chargeFarticle(fe,&nbart);
Showtart(tart,nbart);
printf("Ref: ");
scanf("%s",l.ref);
printf("pu: ");
scanf("%f",&l.pu);
printf("qt: ");
scanf("%d%*c",&l.qt);
printf("Desi: ");
fgets(l.desi,31,stdin);
l.desi[strlen(l.desi) -1]='\0';
tart=AddArt(l,tart,&nbart);
Showtart(tart,nbart);
fclose(fe);
}
Article readArt(FILE * fe)
{
Article a;
fscanf(fe,"%s%f%d",a.ref,&a.pu,&a.qt);
fgets(a.desi,31,fe);
a.desi[strlen(a.desi) - 1]='\0';
return a;
}
Article * chargeFarticle(FILE*fe,int *nbart)
{
Article *tart;
int i;
fscanf(fe,"%d",nbart);
tart=(Article *)malloc(*nbart*sizeof(Article));
if(tart==NULL)
{
printf("Pb Malloc\n");
exit(1);
}
for(i=0;i<*nbart;i++)
tart[i]=readArt(fe);
return tart;
}
Article * AddArt(Article l,Article *tart,int *nbart)
{
int i;
Article *aux;
aux=(Article *)realloc(tart,*nbart*sizeof(Article));
free(tart);
tart=aux;
tart[*nbart]=l;
*nbart+=1;
return tart;
}