- Another variable type in the C language
is the structure.
It's actually a combination of existing variable types
all tied together into a single unit.
A structure variable simply has many parts,
like a record in a database.
This movie presents the concept of the structure.
You'll see how to set up a structure,
create structure variables,
and how to access structure members.
I'll demonstrate how to fill data into a structure,
as well as the interesting idea
of placing one structure inside another.
This isn't complete code of course,
but it shows you how a structure can
be created in the C language.
The key word struct is followed by the name
of the structure.
In this case the name is record.
The structure members are contained in curly brackets.
Each ending with a semicolon,
because the whole deal is a statement.
The structure members are variable declarations.
Inside this structure you'll find an int and a float member.
The int member is named account,
the float member is named balance.
By itself the structure declaration merely
creates a type of structure,
like a new type of variable in the code.
To use the structure,
you need to declare a variable of the structure type.
So on line nine I'm going to type struct record my_bank.
This statement creates a variable named my_bank
of the record structure type.
To access the structure's members,
you need to reference both the variable name
as well as the member name.
I'm going to add two new lines.
The structure variable name comes first,
then a dot, then the member name.
They must be assigned values equal to the variable type,
in this case an integer and a floating point value.
Save these changes.
Build and run.
And there's the output from each
of the variables inside the structure.
To create another record structure variable,
called your_bank,
you simply have to echo the existing statements.
You can also fill the members in that structure
simply by copying these statements
and replacing my_bank with your_bank.
You could also preset the information inside a structure.
Open exercise file structure2.
In this code, a structure named person is declared.
A variable of the person type is declared at line nine,
it's named president.
The president structure variable is immediately
assigned values.
The values match the structure member types
and are enclosed in curly brackets.
Run this code to ensure that it works.
One thing to note in a structure is how you assign
a string to a structure member.
Open exercise file structure3.
If you can see what's wrong, great.
Otherwise, build the code and then check the build
log to see the specific error.
Incompatible.
That's enough.
You cannot use the assignment operator with a string.
That's because strings are not variables,
they're arrays.
Here's how to solve it.
And remember to add the string header file.
Save the changes.
Build and run.
Now it works.
You can insert any variable type
into a C language structure,
even another structure.
Open file structure4.
In this code you see definitions for two structures.
The first, date, is defined at line six.
It holds members year, month, and day.
The second structure, person, is declared at line 11.
It contains a name array but also the date structure
in the form of a birthday variable.
A person's structure variable friend is declared at line 15.
Lines 17 through 20 fill the friend variable.
String copy is used to assign the string.
For the structure member birthday,
note how two dots are used to fill in these
substructure members in lines 18, 19, and 20.
That's how it works.
That's how structures within structures are referenced.
Build and run the code.
By the way, most programmers will declare a structure
and then create a structure variable in the same statement.
You can do that here on line 14.
Specify the variable name friend,
the end of line 14,
then remove line 15.
Save the changes.
Build and run.
The output is the same.
I prefer not to use this shortcut in my own code,
in that I find it less readable.
But you're free to use it and you'll see
it quite frequently in other C code.