I'm brushing up on some C programming, and trying to understand why I cannot print a data item from a structure (struct) after dynamically allocating memory to for structure.
I tried printing the data items in the struct to see what values I get but my code does not compile, and I get an error.
#include <stdio.h>
#include <stdlib.h>
typedef struct Collection {
int age;
char date[20];
char name[20];
} Collection;
int main(void) {
int i;
int n = 10;
Collection **dataCollection;
dataCollection = malloc(sizeof(Collection*)*n);
dataCollection->age = 20;
for(i = 0; i < n; i++) {
dataCollection[i] = malloc(sizeof(Collection)*n);
printf("Data collection item: %d\n", dataCollection->age);
}
for(i = 0; i < n; i++)
free(dataCollection[i]);
free(dataCollection);
return 0;
}
I get the following errors:
practice1019.c:18:20: error: member reference base type 'Collection *' (aka 'struct Collection *')
is not a structure or union
dataCollection->age = 20;
~~~~~~~~~~~~~~^ ~~~
practice1019.c:23:56: error: member reference base type 'Collection *' (aka 'struct Collection *')
is not a structure or union
printf("Data collection item: %d\n", dataCollection->age);