Structures with Pointers to Structures
Recall from a previous lecture we created a structure of type PERSON
as
typedef struct person
{
char name[30];
int age;
} PERSON;
But now suppose that we want more than just a 30 (or is it 29??) character
name. Suppose we want to be able to refer to the first name, last name
and even middle initial separately. We could make another structure of
type NAME and use it inside of PERSON. We did something similar to this
in an earlier lecture with POINT and LINE.
typedef struct name
{
char firstName[20];
char middleInitial;
char lastName[20];
} NAME;
and now define PERSON as
typedef struct person
{
NAME name;
int age;
} PERSON;
Recall the syntax
Now, given the following declaration
PERSON bob;
to what do the following expressions refer and what is their data type?
Answers listed below.
bob
bob.name
bob.name.firstName
bob.name.lastName[5]
The memory
Given the declaration of PERSON bob; above, how does the compiler
allocate memory?
All memory needed for the variable bob is contiguous, just as you
would expect. We would also expect that sizeof(PERSON) would be 45 if we
just count up the number of bytes needed. The actual sizeof a PERSON (or any
other type of structure) is not a predictable as you might think. There is
actually wasted space inside most structures. Here's a short program that
proves space is being wasted inside a PERSON.
#include
typedef struct name
{
char firstName[20];
char middleInitial;
char lastName[20];
} NAME;
typedef struct person
{
NAME name;
int age;
} PERSON;
int main()
{
printf("The size of a PERSON is %d bytes\n", sizeof(PERSON));
return 0;
}
The size of a PERSON is 48 bytes
If we chose not to have so much space being used inside the structure, we
can create a different picture of memory. Recall that we can create pointers
to structures. Can we have a member of a structure be a pointer to another
structure? Of course.
Consider this definition of PERSON
typedef struct person
{
NAME *pName;
int age;
}PERSON;
What does the picture of memory for PERSON look like now?
Now the memory to hold the NAME is not part of PERSON. PERSON only
has a pointer to a NAME. Where's the memory that holds the NAME?
The memory for NAME must be malloc'd.
What's the syntax now?
Now that PERSON has a pointer to NAME, the syntax is different.
Given the declaration PERSON bob;
What is the syntax for referring to
a. bob's name
b. bob's first name
c. the 3rd character of bob's last name
Answers below.
Answers to the first set of questions regarding PERSON bob
bob is of type "struct person" and refers to all the
information about bob
bob.name is of type "struct name" representing bob's first
name, last name and middle initial.
bob.name.firstName is an array of 20 characters
(or char *), which can be considered a "string" if we know or assume it has
a terminating '\0' character. It's obviosusly bob's first name.
bob.name.lastName[5] is of type "char" and is the 6th character
of bob's last name
Answers to the questions regarding proper syntax.
If pName is a pointer to "struct NAME" then
bob.pName is a pointer to bob's NAME, and
*bob.pName is of type NAME
bob's first name is bob.pName->firstName which is the
character array
the 3rd character of bob's last name is
bob.pName->lastName[2]