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);
Continuing from the comment, you cannot assign
dataCollection->age = 20;
after merely allocating a block of memory sufficient to hole10
pointers-toCollection
. You have allocated memory for the pointers, but not storage for the stucts themselves.Additionally, you must validate every allocation, e.g.
Now, before you can assign a value to a member of any struct, you must allocate a block of memory to hold the struct and then assign the starting address for that block to one of the pointers you have just allocated, e.g.
(note: above there is no
* n
afterCollection
insizeof(Collection)
-- or you will over-allocate by a factor of10
on struct storage...)Now, you have storage for a struct and you can make use of the members (
age
below)Good job on the
free
of each of the structs and then the pointers. Putting it altogether, you could do:Example Use/Output
Look things over and let me know if you have questions.