c - vector allocation error subscript to pointer to incomplete type -
i have product
typedef struct proditem *proditem;
and have symbol table of products
typedef struct tabp *tabp; struct tabp { proditem vettp; int np; };
i need allocate in memory, :
tab->np = max; // quantity of products tab->vettp = malloc(max * sizeof(*proditem));
but if try use vector tab->vettp have error:
tab->vettp[i]
<<-- subscript pointer incomplete type
can me?
i solved myself, missing pointer *
typedef struct tabp *tabp; struct tabp { proditem *vettp; int np; };
product item.h:
#ifndef proditem_h #define proditem_h #include <stdio.h> #include <stdlib.h> typedef struct proditem *proditem; typedef char* prodkey ; proditem proditemscan(file*); void proditemshow(file*, proditem); int proditemcheckvoid(proditem); void proditemfree(proditem); int proditemgreater(proditem,proditem); prodkey prodkeyget(proditem); int prodkeycomp(prodkey, prodkey); #endif /* proditem_h */
symbol table.h
#ifndef tabp_h #define tabp_h #include <stdio.h> #include "proditem.h" typedef struct tabp *tabp; tabp tabpscan(file*); void tabpshow(file*,tabp); void tabpfree(tabp); int tabpcount(tabp); int tabpsearch(tabp, prodkey); #endif /* tabp_h */
symbol table.c
#include "tabp.h" struct tabp { proditem *vettp; int np; }; tabp tabpscan(file *f) { int max; fscanf(f,"%d", &max); tabp tab = malloc((sizeof(*tab))); tab->np = max; tab->vettp = malloc(max * sizeof(proditem)); for(int i=0; i<max; i++) { tab->vettp[i] = proditemscan(f); } return tab; } void tabpshow(file *f, tabp tab){ for(int i=0; i<tab->np; i++) { proditemshow(f, tab->vettp[i]); } } int tabpcount(tabp tab) { return tab->np; } int tabpsearch(tabp tab, prodkey key) { (int i=0; i<tab->np; i++) { if (prodkeycomp( prodkeyget(tab->vettp[i]), key)==1){ return i; } } return -1; }