250 Intermediate C Programming
16.1 Struct and Object
When multiple pieces of data are organized together, it is a structure. A structure is a
type (similar to int or char). After creating the structure, the type can be used to create
an object (borrowing a term from C++ and Java). An object is a specific instance of a type.
For example, “bicycle” is a type describing the properties: two wheels, gears, etc. A given
bicycle is an instance that has these properties. Borrowing another term used in C++ and
Java, we call each piece of data an attribute. For a bicycle, the wheel size is an attribute.
The brand is another attribute. These three terms are further described below:
• Structure: a data type so that multiple pieces of data can be organized together. The
piece may have different types (int, char, or even other structures).
• Object: a specific instance of a structure. In the example below, int and t are both
types. Suppose t is a structure. Using these types, x is a specific instance of integer,
and y is a specific instance of type t.
int x;1
t y ;2
• Attribute: A structure organizes different pieces of data together. Each piece of data
is referred to as an attribute. The attributes store the information specific to the
particular object. The attributes’ values of different objects are likely different.
It is important to clearly distinguish these three concepts (structure, object, and at-
tribute). Here are some more examples:
1. “Person” can be a structure. Individual people are specific instances of Person. Let’s
call two specific people: “Alice” and “Bob”. Their names, age, height, and phone
numbers are attributes. The attributes of “Alice” are different from those of “Bob”.
They have the same attributes, but those attributes have different values, and are
stored in different locations in memory.
2. “Car” is a structure. Every car has some attributes, such as year, brand, and color.
Your car is a particular instance and it is an object. That means it has a particular
year, brand, and color that can be different from my car.
3. “Desk” is a structure. Every desk has attributes, such as width, height, number of
drawers, weight, material, etc. The desk in your office is an instance. It is a particular
desk with specific values for those attributes that can be different from the desk at
your home.
Here is another way to think about the relationship between structure and object: A
structure describes what attributes (age, height, color, etc.) an object has. An object has
specific values for those attributes that can be distinguished from any other object of the
same structure. Furthermore, an object is stored in memory somewhere. A structure has no
values for its attributes, it is not stored in memory.
Now that we know what structures, objects, and attributes are, how do we create them?
Below is an example that creates a new type for vectors. A vector has three components:
x, y, and z. It is desirable to create a new type called Vector and put these attributes
together. Programmers often create new structure types by using typedef struct. The
structure’s name is given at the end of the structure, after } and before ;.
// vector . h1
#i f n d e f VECTOR_H2
#d ef in e VECTOR_H3
typedef s t ru ct4
{5