che casino, ora decifro![]()
Grazie ancora, alla prossima
Originariamente inviato da andbin
Questo esempio l'ho scritto velocemente (non l'ho provato) ma dovrebbe essere ok.
Poi, dove ne hai bisogno, fai:codice:typedef struct { int a; int b; } ELEM, *PELEM; typedef struct { PELEM pelem; int size; } ELEMARRAY, *PELEMARRAY; int ElemArray_Create (PELEMARRAY parray, int size) { PELEM pelem; if (parray == NULL) return 0; pelem = (PELEM) malloc (size * sizeof (ELEM)); if (pelem == NULL) return 0; parray->pelem = pelem; parray->size = size; return 1; } void ElemArray_Free (PELEMARRAY parray) { if (parray == NULL) return; if (parray->pelem != NULL) free (parray->pelem); parray->pelem = NULL; parray->size = 0; } int ElemArray_Realloc (PELEMARRAY parray, int newsize) { PELEM pelem; if (parray == NULL) return 0; pelem = (PELEM) realloc (parray->pelem, newsize * sizeof (ELEM)); if (pelem == NULL) return 0; parray->pelem = pelem; parray->size = newsize; return 1; }
Se ti stai chiedendo perché ho scritto tutto questo codice ... beh, così la gestione dell'array espandibile è molto più "pulita" e "incapsulata"!!!codice:ELEMARRAY mioarray; if (ElemArray_Create (&mioarray, 100)) { .... mioarray.pelem[0].a = 10; mioarray.pelem[0].b = 20; .... if (ElemArray_Realloc (&mioarray, 200)) { .... } ElemArray_Free (&mioarray); }
Naturalmente puoi aggiungere altre funzioni.

Rispondi quotando