2016-06-02 7 views
-4

ich habe mein ProduktVektor Zuordnungsfehler Index auf unvollständigen Typen auf Zeiger

typedef struct proditem *prodItem; 

und ich habe eine Symboltabelle von Produkten

typedef struct tabp *tabP; 
struct tabp { 
    prodItem vettP; 
    int nP; 
}; 

i im Speicher zuweisen müssen, dann:

tab->nP = max; // quantity of products 
tab->vettP = malloc(max * sizeof(*prodItem)); 

aber wenn ein Versuch, die Vektorregisterkarte zu verwenden-> vettP den Fehler haben:

tab->vettP[i] < < - Index auf unvollständigen Typen auf Zeiger

kann mir jemand helfen?

+2

Und was ist 'struct prodItem'? Du tippst eine 'struct' als' pointer' ein. – user3078414

+2

Sind alle diese Codes in der gleichen Datei? Einschließlich 'struct prodItem' - für mich klingt es so, als ob' struct prodItem' nicht bekannt ist, wenn Sie den Code 'tab-> vettP [i]' – 4386427

+2

Typedef Zeiger ist eine schlechte Idee: meine persönliche Meinung. BTW Tab-> vettP = malloc (max * sizeof (* prodItem)); 'sollte' tab-> vettP = malloc (max * sizeof (struct proditem)); ' – LPs

Antwort

-1

i gelöst mich, vermisst einen Zeiger *

typedef struct tabp *tabP; 
struct tabp { 
    prodItem *vettP; 
    int nP; 
}; 

Produkt 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) { 
    for (int i=0; i<tab->nP; i++) { 
     if (prodKeyComp(prodKeyGet(tab->vettP[i]), key)==1){ 
      return i; 
     } 
    } 
    return -1; 
} 
+0

Sind Sie sicher? ? Jetzt haben Sie einen Zeiger des Zeigers. Nichts in Bezug auf den Fehler, den Sie gepostet haben ... – LPs

+0

Ja, ich werde den Beitrag mit dem ganzen Code aktualisieren –

+2

Seien Sie vorsichtig bei 'typedef'ing pointers.Wie LPs darauf hingewiesen haben, verursachen sie im Allgemeinen mehr Probleme, indem das Niveau der Indirektion verschleiert wird, als sie jemals durch das Speichern von Tipparbeit lösen. –

Verwandte Themen