BP-5-3 Structure Type

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值