This example declares a structure. The new type is named Employee:
struct Employee // Employee is a new type
{
char firstName[10]; // first name is a char array
char lastName[12]; // last name is char array
char middleInitial; // middle initial is char
float salary; // yearly pay is float
int age; // age is int
int years; // years of service is int
};
Now, we can use it to declare a variable of this new type:
Employee clerk; // clerk is an Employee
This is what the structure looks like in memory.
clerk.firstName ® ? ? ? ? ? ? ? ? ? ?
clerk.lastName ® ? ? ? ? ? ? ? ? ? ? ? ?
clerk.middleInitial ® ?
clerk.salary ® ? ? ? ?
clerk.age ® ? ?
clerk.years ® ? ?
- Note that all of the values are currently undefined. We haven't initialized any of the member variables yet.
- To access individual members of the structure, use the dot notation (e.g. clerk.age, clerk.years)
- The name clerk by itself refers to the entire structure.