Structures, Unions, Enumerations, and Type
Definitions
(構造体 (struct)、共用体
(union)、列挙型 (enum)、typedef)
Computing Practice I
6th lecture, May 25, 2017
http://www.sw.it.aoyama.ac.jp/2017/CP1/lecture6.html
Martin J. Dürst
© 2005-17 Martin
J. Dürst 青山学院大学
Today's Schedule
- Minitest
- About last week's exercises
- Structures (
struct
)
- Type declarations (
typedef
)
- Unions (
union
)
- Enumeration types (
enum
)
- About today's exercises
ミニテスト
- 授業開始までにログイン済み
- 授業開始まで教科書、資料、筆箱、財布などを鞄に入れ、鞄を椅子の下に
- テスト終了後その場で待つ
Applications of C: gcc
- GNU Compiler Collection, formerly the
GNU C Compiler
- First published in 1987
- Input: C, C++, Objective-C, Fortran, Java, Ada, Go,...
- Output for Intel x86, ARM, and over 30 other architectures
- Size of actual program: about 1'750'000 lines, tests: about 550'000
lines
- Uses C++ starting with version 4.8
Results of Previous Exercises
|
05A1 |
05B1 |
05B2 |
05B3 |
05C1 |
05C2 |
100 points |
97 |
92 |
91 |
64 |
26 |
10 |
60 points |
1 |
6 |
7 |
34 |
61 |
73 |
error |
- |
- |
- |
- |
11 |
8 |
not submitted |
1 |
1 |
1 |
1 |
1 |
8 |
前回の演習問題について
- 発展問題はともかく、そうでない問題は必ず提出
(これからも使う内容の演習)
- 予習が非常に大事
- Q&A フォーラムはもっと活用
前回の演習: 05C1
- 短い関数が多い
- 関数の名前は長くて分かりやすい
- 関数のおかげで「何を」と「どうやって」を分離
- 大学・学生関連の関数と再利用可能な関数は区別
- 学科の判断を
switch
(プログラム)
からテーブル (データ) に変更
前回の演習: 05C2
main
関数は超簡単
- 再帰関数は
- 一行入力 (入力が終了の時、そのまま
return
)
- 再帰的呼び出し
- 入力した一行を出力
- バッファは関数内のため、呼び出しごとに別途に用意
- 行の逆順の出力は再帰呼び出しのパターンによって実現
自覚: 分かることと分からないこと
本授業について、一番分かることと一番分からないことそれぞれ三つ列挙しなさい。
Importance of Data
- Lifetime:
- Hardware: 3-5 years
- Software: about 10 years
- Data: more than 30 years (examples: graduation certificate, social
security)
- In Information Technology, the design of data structures is more
important than the design of functions
Main Data Types in C
Basic Types
- Integers/characters (
char
, short
,
int
, long
)
- Floating point numbers (
float
, double
)
Derived Types
- Arrays, functions
- Structures (
struct
), unions (union
),
enumeration types (enum
)
- Pointers
Simple Example of a Structure (struct
)
typedef struct { // declaration of type Person
char name[40];
double height; /* m */
int weight; /* kg */
} Person;
Person student = { "Taro Aoyama", 1.70, 70 }; // definition and initialization
double bmi = student.weight / // use
(student.height * student.height);
Structure
- Data type combining several kinds of data
- The parts of a structure are called members
- Operations possible on whole structures:
- Declaration and definition of variables, initialization
- Assignment
- Function arguments and return values
- Operations that can only applied to members (not to whole structures):
- Calculations
- Input and output
- Functions abstract functionality, structures abstract data types
[Object orientation combines both abstractions into one abstraction]
Giving a New Name to a Type: typedef
(see textbook pp. 272-273)
typedef
for types other than struct
- Naming a type
- Program becomes (shorter and) clearer
- Good idea to create a convention to distinguish type names from
variable/function names(example: variable names: lower case; type names:
start with upper case)
Definition and Use of Type Names (typedef
)
Example: 4×4 matrices (used for 3D graphics, games,...)
- Define a variable with the intended type
Ex.: double a[4][4];
- Replace variable name with type name
Ex.: double Matrix[4][4];
- Add the
typedef
keyword at the start to complete the
typedef
declaration
Ex.: typedef double Matrix[4][4];
- Use
typedef
in the same way as preexisting types
Ex.: Matrix a; Matrix mul(Matrix, Matrix); ...
For Reference: Different Ways to Declare Structures
- Historic original (using a structure
tag name)
struct Person
{...};
(not allowed in this lecture)
- Definition using structure tag
name, followed by separate
typedef
struct Person
{...};
typedef struct Person
Person;
(not allowed in this lecture)
(structure tag name and
typedef
type name may be the same or different)
- Combining structure tag name
and
typedef
typedef struct Person
{...} Person;
(tolerated in this lecture)
- Omitting the structure tag
name (recommended)
typedef struct {...} Person;
(preferred in this lecture)
Ways of using the above declarations:
Person p1;
(possible with 2., 3., and 4.)
struct Person
p1;
(possible with 1., 2., and 3.)
- Leftover from early C history
- Unnecessary except for recursive data structures (e.g. lists,
trees)
Structures and Functions
double bmi (Person p)
{
return p.weight
/ (p.height * p.height);
}
- Structures can be used both as arguments and as return values
- Pass by value is used in both cases (similar to
int
)
Example of Structures as Arguments and Return Values
Complex add(Complex a, Complex b)
{
Complex result;
/* calculate the real/immaginary parts of result
from the real/immaginary parts of a and b */
return result;
}
Unions (union
)
- Declaration syntax is the same as for
struct
, but
struct
s reserve separate memory for each member
union
s superimpose members
→ only one member can be used at one time
- Applications:
- Memory savings
- Low-level data conversion
- Simulation of object-orientation
Enumeration Types (enum
)
- Used to distinguish a small number of things
Examples: Moves in rock, paper, scissors; colleges/departments; types of
bank accounts,...
- Making program easier to understand by using names (instead of
numbers)
- In C,
enum
s are implemented as integers
(int
)
- Similar concepts available in many other language
(Ruby: Symbol
, Lisp: Atom,...)
Example of Enumeration Type
Make 03C2 and 03C3 (rock,
paper, scissors) easier to read:
typedef enum
{ Rock, Scissors, Paper } Janken;
Janken moveA, moveB;
...
if (moveA == Paper)
pointA += 5;
Additional Explanations about Exercises
演習問題の概要
- 06A1: 日付のための構造体
- 06A2: 二つの点の間の距離
- 06B1: 複素数の足し算
- 06C1: 成績表
- 06C2: 複素数の関数による計算 (重要:
来週・再来週に再利用)
- 06C3 (発展問題、月曜締切): 趣味の数のグラフ
入出力の詳細に注意
次回までの準備
- 残りの演習問題を宿題として完成、提出
- 分からないことについてしっかり復習
- 来週の総合復習テスト (紙、約 30 分) の準備
Glossary
- structure
- 構造体
- union
- 共用体
- enumeration type
- 列挙型
- lifetime
- 寿命
- graduation certificate
- 卒業証明証
- social security
- 社会保障
- basic type
- 基本型、基本的なデータ型
- derived type
- 派生型
- floating point numbers
- 浮動小数点数
- pointer
- ポインタ
- member (of a structure)
- (構造体の) メンバ
- convention
- 決まり、慣例
- pass by value
- 値渡し
- complex number
- 複素数
- real part
- 実部
- immaginary part
- 虚部
- superimpose
- 重ねる
- data conversion
- データ変換
- object-orient(ed/ation)
- オブジェクト指向
- naming
- 命名
- matrix (複数 matrices)
- 行列
- structure tag name
- 構造体タグ (名)