Structures, Unions, Enumerations, and Type Name Definitions

(構造体 (struct)、共用体 (union)、列挙型 (enum)、typedef)

Computing Practice I

6th lecture, May 17, 2018

http://www.sw.it.aoyama.ac.jp/2018/CP1/lecture6.html

Martin J. Dürst

AGU

© 2005-18 Martin J. Dürst 青山学院大学

Today's Schedule

 

ミニテスト

 

Applications of C: gcc

 

Results of Previous Exercises

05A1 05B1 05B2 05B3 05C1 05C2
100 points 91 67 73 45 16 4
60 points 9 33 27 53 61 56
error - - - 1 12 18
not submitted - - - 1 11 22

 

前回の演習問題について

 

前回の演習: 05C1

 

前回の演習: 05C2

 

自覚: 分かることと分からないこと

本授業について、一番分かることと一番分からないことそれぞれ三つ列挙しなさい。

 

Importance of Data

 

Main Data Types in C

Basic Types

Derived Types

 

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

 

Giving a New Name to a Type: typedef

(see textbook pp. 272-273)

 

Definition and Use of Type Names (typedef)

Example: 4×4 matrices (used for 3D graphics, games,...)

  1. Define a variable with the intended type
    Ex.: double a[4][4];
  2. Replace variable name with type name
    Ex.: double Matrix[4][4];
  3. Add the typedef keyword at the start to complete the typedef declaration
    Ex.: typedef double Matrix[4][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

  1. Historic original (using a structure tag name)
    struct Person {...}; (not allowed in this lecture)
  2. 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)
  3. Combining structure tag name and typedef
    typedef struct Person {...} Person; (tolerated in this lecture)
  4. Omitting the structure tag name (recommended)
    typedef struct {...} Person; (preferred in this lecture)

 

Ways of using the above declarations:

 

Structures and Functions

double bmi (Person p)
{
return p.weight
/ (p.height * p.height);
}

 

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)

 

Enumeration Types (enum)

 

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

 

演習問題の概要

入出力の詳細に注意

 

次回までの準備

 

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
構造体タグ (名)