Chapter 05 Compound Data Type - Constructed Type
3. Structure Type
Structure type is a kind of compound data type constructed by the user, which is composed of several members known as its member.
3.1 Definition of Structure Type
struct <name-of-structure-type> {<members-list>};
Name of structure type is an identifier, and member list is description for each member, which is quite similar to variable definition. The data type of the structure’s members can be any data type except void and itself. Different structure type can have member of the same name.
Definition for the variable of a structure type is just as usual.
<type-name> <variable-name>;
Initialization for a structure type is quite similar to initialization for an array. The example below will perfectly demonstrate how to define and initialize a structure type variable. Remember that we can’t initialize a member of a structure type when defining it because memory will not allocate room when we define a data type, which means there’s no room to store the initial value.
Moreover, of course we can define a structure type without name, but if so we have to define some variables at the same time or the definition of structure type is useless.
enum Sex {MALE, FEMALE};
struct Date {
int year, month, day;
};
enum Major {MATHEMATICS, PHYSICS, CHEMISTRY, COMPUTER, GEOGRAPHY, ASTRONOMY, ENGLISH, CHINESE, PHILOSIPHY};
struct Student {
int no;
char name[20];
Sex sex;
Date birth_date;
char birth_place[40];
Major major;
};
Date today, yesterday, some_date;
Student monitor, best_student;
Student some_student = {2, "Jacy", Male, {2002, 5, 1}, "Yancheng", COMPUTER};
3.2 Manipulation for Structure Type
-
Access to Members
<variable-name>.<member-name>
Dot expression is a universal kind of expression used for access to members of a structure type variable or attributes of an instance or an object. The following examples are legal and via observation you’ll find the rules.
best_student.no = 1; strcpy(best_studenr.name, "Jerry"); best_student.sex = MALE; best_student.birth_date = some_date; strcpy(best_student.birth_place, "Nanjing"); best_student.major = COMPUTER;
-
Assignment for Structure Type
Variables of the same structure type can be assigned to each other.
-
Passing a Structure Type to a Function
Just the same as usual:
<type-name> <parameter-name>
in the function signature and<argument-name>
in the function call. -
How a Structure Type Stored in the Memory
Just like an array, while the only difference is that the sizes of rooms are various according to the types of its members and we use dot expression instead of subscript to get access to them.
3.3 Application of Structure Type
- Name-table and its Research
const int NAME_LEN = 20;
const int TABLE_LEN = 100;
struct TableItem { //element-type of a name-table
char name[NAME_LEN]; //key
...... //other information
} ;
TableItem name_table[TABLE_LEN]; //name-table
#include <cstring>
using namespace std;
int linear_search(char key[], TableItem t[], int num_of_items){
int index;
for (index = 0; index < num_of_items; index++)
if (strcmp(key, t[index].name) == 0) break;
if (index < num_of_items)
return index;
else
return -1;
}
#include <cstring>
using namespace std;
int binary_research(char key[], TableItem t[], int num_of_items) { //if the nametable has been sorted by key in advance
int index, first = 0, last = num_of_items - 1;
while (first <= last) {
index = (first + last) / 2;
int r = strcmp(key, t[index].name);
if (r == 0)
return index;
else if (r > 0)
first = index + 1;
else
last = index - 1;
}
return -1;
}