A
Alfonso Morra
Hi,
I'm trying to write a simple generic container. I have the ff:
typedef union {
long int_val ;
char* string_val ;
double dbl_val ;
void* vptr_val ;
}Element ;
I want to be able to store vars of type Element in a dynamic (i.e.
resizable) array . I tried the ff:
#define MAX_NUM 3
int main(int argc, char* argv[]) {
Element* data ;
int i ;
for (i=0;i< MAX_NUM; i++) {
data++ = (Element*)calloc(1, sizeof(int)) ;
*(data) = i ;
}
for (i=0;i< MAX_NUM; i++)
printf("Element %d has value : %d", i,*(data)) ;
for (i=0;i< MAX_NUM; i++)
free(data) ;
}
It is obviously wrong (illegal indirection etc). This is schoolboy stuff
and embarassingly, I've forgotten how to fix it! (using C++ and STL
libraries for too long!).
Any help or pointers (pun intended) much appreciated
I'm trying to write a simple generic container. I have the ff:
typedef union {
long int_val ;
char* string_val ;
double dbl_val ;
void* vptr_val ;
}Element ;
I want to be able to store vars of type Element in a dynamic (i.e.
resizable) array . I tried the ff:
#define MAX_NUM 3
int main(int argc, char* argv[]) {
Element* data ;
int i ;
for (i=0;i< MAX_NUM; i++) {
data++ = (Element*)calloc(1, sizeof(int)) ;
*(data) = i ;
}
for (i=0;i< MAX_NUM; i++)
printf("Element %d has value : %d", i,*(data)) ;
for (i=0;i< MAX_NUM; i++)
free(data) ;
}
It is obviously wrong (illegal indirection etc). This is schoolboy stuff
and embarassingly, I've forgotten how to fix it! (using C++ and STL
libraries for too long!).
Any help or pointers (pun intended) much appreciated