- When your program needs memory
you can allocate it on the fly.
That includes memory for any variable type array
or even a structure.
In this movie, I demonstrate how structures
can be created using pointers
with their memory allocated as the program runs.
In this code, storage for a structure is allocated.
The structure has to be defined first,
which happens at line six.
Remember, that a structure itself, isn't a variable.
It's simply a declaration of what
the contents of a structure would be.
In this case, the members are month, day, year.
At line 11, a pointer variable
for that kind of structure is created.
The pointer is a memory location.
It's not the structure.
Storage for the structure is allocated at line 13,
courtesy of the malloc function.
See how the "sizeof" operator is used
to allocate enough storage for the structure itself.
And the structure's name is used,
not the structure pointer variable.
Assuming that the structure is created,
lines 20 through 25 fill the structure.
Because the structure is referenced by a pointer,
the "structure pointer member" operator is used.
It's a -> operator
Even so, the variable being referenced
by this operator isn't a pointer.
So the & is required inside the scanf function.
The printf statement at line 27 displays the results.
Build and run the code.
I'll type in my own birthday info.
And there it is.
This type of memory allocation for a structure
plays a key role in length lists,
which is a topic for a later movie.