Array mit Strukturen



  • Hi Leute,

    Ich brauche Hilfe:

    Ich habe Strukturen:

    typedef struct freq freq_t;
    typedef struct lut lut_t;
    
    typedef struct freq {
    
      long instr;
      long frequency;
    };
    
    typedef struct lut {
      	freq_t  tab[10];
    	int clenght;
    	long entries;
    	long rest;
      long posComSize;
    };
    

    Mit solcher Definition, habe Ich eine richtige Darstellung von array mit strukturen:

    lut_t Tables[1]{
    freq_t tab[0] {instr, frequency}
    freq_t tab[1] {instr, frequency}
    freq_t tab[2] {instr, frequency}
    freq_t tab[3] {instr, frequency}
    freq_t tab[4] {instr, frequency}
    .....
    freq_t tab[19] {instr, frequency}
    }

    Hier habe Ich Platz für freq_t tab[20] normal resorviert.

    Aber wenn Ich so was habe

    typedef struct lut {
      	freq_t  *tab;
    	int clenght;
    	long entries;
    	long rest;
      long posComSize;
    };
    
    Tables[1].tab = (freq_t *) calloc (20, sizeof(freq_t));
    

    kriege Ich:

    lut_t Tables[1]{
    freq_t tab {instr, frequency}
    }
    

    Ich muss dynamisch Speicher reservieren, da Ich die Anzhal der Elemente im Array
    tab sich ändert.

    Was mache Ich falsch?

    Hilft mir bitte !!!!!!!!!!!!!!!!!!

    lena



  • Bei deinem oberen Beispiel scheint irgendwas nicht zu stimmen - in der lut-Struktur ist nur Platz für 10 freq_t-elemente.

    Bei der Variante mit der dynamischen Speicherverwaltung enthält dein Objekt tatsächlich nur einen Pointer auf ein freq_t - und dort wo der hinzeigt, werden hintereinander weg die einzelnen Elemente in den Speicher gepackt:

    Tables:
      tab     0x....
      clength 20
      entries 15
      rest    5
    
    Speicher 0x....
      tab[0]: instr/frequency
      tab[1]: instr/frequency
      ...
      tab[19]:instr/frequency
      tab[20]:undef
    

    (PS: Dir ist hoffentlich klar, daß du zu deinem calloc() auch irgendwo ein free() benötigst)



  • Was Du falsch machst? Irgendwie hast Du keine richtige Frage gestellt. 😉

    Was ist "richtige Darstellung", und vor allem, wer (zum Geier 😃 ) macht diese "Darstellung"?


Anmelden zum Antworten