Arrays: collections of homogeneous (similar type) objects
Structures (or simply Structs): collections of heterogeneous (different
types) objects
Definition: A structure is a collection of related values. Structures
are constructed using objects of other types. Structures may have a structure
tag name, as well as structure instances.
Examples to illustrate the need:
- Employee records (name, age, salary, etc)
- employeeParallelArrays.cpp
- using parallel arrays (inconvenient when we have many fields)
- employee0.cpp
- storing information for a single employee in a struct, initializing
at declaration time
- employee1.cpp
- storing information for multiple employees into an array of structs
- employeeNested.cpp
- nesting an address struct in an employee struct.
- employeeNested2.cpp
- similar to the above, only now we add a function to search by name for
a particular employee. Even though you can't
return an array from a function, you can return a struct, as shown in
this example. The returned struct can be used in an assignment statement.
- Advanced:
- employee2.cpp
- storing employee information using a struct pointer
- employee3.cpp
- passing a pointer to a struct to a function. Declare a new type using
typedef.
- See a Word document with the above examples in a single file
- Maze problem, keeping track of board contents and cameFrom
[CS Dept] [UIC]
[Prof. Reed]