Reputation: 13
I'm having this error "Dereferencing pointer to incomplete type" in my main.c, line 14, when I try to use the pointer to the struct for the first time (in main). These are my files:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "struttura_dati.h"
int main(int argc, char *argv[])
{
Prodotti *prodotti;
int i, k, nc=0;
k=atoi(argv[1]);
prodotti=leggi_medicinali();
printf("%d", prodotti->categorie[0]);
free(prodotti); /*da liberare anche vett */
return 0;
}
struttura_dati.h
#ifndef STRUTTURA_DATI_
#define STRUTTURA_DATI_
#include <stdlib.h>
#include <stdio.h>
#define MAXNOME 21 /* max lunghezza nomi */
#define MAXPROD 50 /* max numero prodotti */
#define MAXDITT 10 /* max numero ditte */
typedef struct strutt_dati medicinale;
typedef struct vett_struct Prodotti;
Prodotti* leggi_medicinali();
int cerca(char temp[MAXNOME], char database[MAXPROD][MAXNOME], int n);
void stampa(char stringa[]);
#endif
struttura_dati.c
#include "struttura_dati.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct strutt_dati {
char nome[MAXNOME];
int ditta;
int categoria;
};
struct vett_struct {
medicinale *vett;
char ditte[MAXDITT][MAXNOME];
char categorie[MAXPROD][MAXNOME];
int n_categorie;
int n_ditte;
};
Prodotti* leggi_medicinali() {
int i=0, fl;
char temp_categ[MAXNOME], temp_ditta[MAXNOME];
FILE *fp;
Prodotti* leggi_medicinali() {
int i=0, fl;
char temp_categ[MAXNOME], temp_ditta[MAXNOME];
FILE *fp;
Prodotti* prodotti;
fp=fopen("Medicinali.txt", "r");
if(fp==NULL) {
printf("Errore nell'apertura del file ''Medicinali.txt''\n");
exit(EXIT_FAILURE);
}
prodotti=malloc(MAXPROD*sizeof(Prodotti*));
prodotti->vett=malloc(MAXPROD*sizeof(medicinale));
prodotti->n_categorie=0;
prodotti->n_ditte=0;
while(!feof(fp)) {
fscanf(fp, "%s %s %s", temp_categ, prodotti->vett[i].nome, temp_ditta);
fl=cerca(temp_categ, prodotti->categorie, prodotti->n_categorie);
if(fl==-1) {
strcpy(prodotti->categorie[prodotti->n_categorie], temp_categ);
prodotti->vett[i].categoria=prodotti->n_categorie;
prodotti->n_categorie++;
}
else
prodotti->vett[i].categoria=fl;
fl=cerca(temp_ditta, prodotti->ditte, prodotti->n_ditte);
if(fl==-1) {
strcpy(prodotti->ditte[prodotti->n_ditte], temp_ditta);
prodotti->vett[i].ditta=prodotti->n_ditte;
prodotti->n_ditte++;
}
else
prodotti->vett[i].ditta=fl;
i++;
}
printf("%s ", prodotti->categorie[0]); /* THIS, IN struttura_dati.c WORKS, BUT IT DOESN'T IN main.c */
fclose(fp);
return prodotti;
}
Could you please help me understand why the main.c can't use properly everything with prodotti->(something)? Thanks!
Upvotes: 1
Views: 2844
Reputation: 2746
The way the code is currently structured, I think you'll have a NULL referencing problem even if you solve your current issue. In your main.c you have declared,
Prodotti *prodotti;
which you (presumably) fill with;
prodotti=leggi_medicinali();
But have a look at the function as its currently quoted;
Prodotti* leggi_medicinali() {
int i=0, fl;
char temp_categ[MAXNOME], temp_ditta[MAXNOME];
FILE *fp;
Prodotti* prodotti;
(... things that works)
return;
}
See that final "return" ? What does it really do? Does it return anything? It just shifts control back to the main function which is left wanting for a return value. I'm actually surprised that the compiler didn't pick this up.
Upvotes: 0
Reputation: 16718
These definitions:
struct strutt_dati {
char nome[MAXNOME];
int ditta;
int categoria;
};
struct vett_struct {
medicinale *vett;
char ditte[MAXDITT][MAXNOME];
char categorie[MAXPROD][MAXNOME];
int n_categorie;
int n_ditte;
};
need to be in struttura_dati.h
, not struttura_dati.c
. Currently, when you use the structs in main.c
the definitions are not visible.
Upvotes: 1