I'm having another problem now, I switched the code that loads to the memory to a separate function, this one:
Code: Cpp
void memoria(FILE *fp, veiculo_t registo, veiculo_t *memptr)
{
fp=fopen(FICHEIRO,"rb");
if (fp==NULL) printf("Impossivel aceder ou criar ficheiro\n\n");
else {
int c=0;
while(fread(®isto,sizeof(registo),1,fp) != 0)
{
memptr = realloc(memptr, (c+1)*sizeof(registo));
memcpy(memptr[c].matricula, registo.matricula, 6);
memcpy(memptr[c].proprietario, registo.proprietario, sizeof(registo.proprietario));
memptr[c].apagado = registo.apagado;
c++;
}
printf("\nFicheiro Carregado para a Memoria\n\n");
fclose(fp);
fflush(fp);
}
}
And in main, I call it as soon as the program opens it:
Code: Cpp
int main()
{
int opcao;
FILE *fp;
veiculo_t registo;
veiculo_t *memptr = NULL;
memoria(fp, registo, memptr);
fwrite(memptr[0].matricula, 6,1,stdout);
fwrite(memptr[0].proprietario, strlen(memptr[1].proprietario),1,stdout);
I get a segmentation fault on those fwrites in main(), I know the problem is somewhere in the use of pointers but isn't the memptr supposed to be passed as a pointer to the function?