バイオプログラミング第一 - github pages · 2020-07-06 · 構造体...
Post on 11-Jul-2020
1 Views
Preview:
TRANSCRIPT
バイオプログラミング第一
榊原 康文、佐藤 健吾
慶應義塾大学理工学部 生命情報学科
構造体
データ型の異なる複数のデータを1つにまとめ,1個の
変数のようにして扱うためのデータ型
例えば,1人の生徒のデータは
u名前
u入学年度
u学科
u学籍番号
u身長
u体重
文字配列
int型
char型
int型
float型
float型
構造体(1) 構造体の定義
構造体の「型の定義」 + 構造体の「変数の宣言」
① 構造体の型定義
struct 【構造体タグ名】{【データ型】 【メンバ名1】;【データ型】 【メンバ名2】;
・・・【データ型】 【メンバ名n】;
};
② 構造体変数の宣言
struct 【構造体タグ名】 【構造体変数名】;
構造体
(1) 構造体の定義
例)
struct DATE_DATA{
int year;
int month;
int day;
};
struct DATE_DATA dx;
型定義
変数宣言
構造体
(1) 構造体の定義
struct DATE_DATA{
int year;
int month;
int day;
};
struct DATE_DATA dx;
year month day
dx
int型 int型 int型
dx.year dx.month dx.day
構造体
(1) 構造体の定義
③ 構造体の型定義と変数の同時宣言
struct 【構造体タグ名】
{
【データ型】 【メンバ名1】;
【データ型】 【メンバ名2】;
・・・
【データ型】 【メンバ名n】;
} 【構造体変数名】;
構造体
(1) 構造体の定義
例)
struct DATE_DATA{
int year;
int month;
int day;
} dx;
③ 構造体の型定義と変数の同時宣言
struct DATE_DATA{
int year;
int month;
int day;
};
struct DATE_DATA dx;
構造体
(2) 構造体の参照,代入
【構造体変数名】.【メンバ名】
ピリオド
struct DATE_DATA{
int year;
int month;
int day;
};
struct DATE_DATA dx;
dx.year=2006;dx.month=5;dx.day=24;y=dx.year;
構造体
struct DATE_DATA{
int year;
int month;
int day;
};
struct DATE_DATA dx;
year month day
dx
int型 int型 int型
dx.year dx.month dx.day
プログラム例
#include <stdio.h>#include <string.h>int main(void)
{struct STUDENT_DATA{int ent_year;char dept;int id;char name[20];float height;float weight;
};
struct STUDENT_DATA x;
x.ent_year=2002;x.dept=’L’;x.id=110;strcpy(x.name,”Sakakibara”);x.height=180.0;x.weight=72.5;
return 0;}
(左から続く)
構造体
ent_year dept id
int型 char型 int型
struct STUDENT_DATA x;
char型 float型 float型
name[20] height weight
x.ent_year x.dept x.id x.name[20] x.height x.weight
構造体
(3) 構造体の初期化
struct 【構造体タグ名】 【構造体変数名】={初期値,・・・};
struct DATE_DATA{
int year;
int month;
int day;
};
struct DATE_DATA dx={2006,5,24};
構造体のメンバの先頭から順番に値が代入される
構造体(4) 構造体の中の構造体
例1) struct PERSON{char name[20];struct DATE_DATA{int year;int month;int day;
} birth;float height;float weight;
};
構造体のメンバに構造体を使うことができる
① 宣言
構造体(4) 構造体の中の構造体
例2) struct DATE_DATA{int year;int month;int day;
};
① 宣言
struct PERSON{char name[20];struct DATE_DATA birth;float height;float weight;
};
構造体(4) 構造体の中の構造体
struct PERSON x;
x.birth.year=1960;x.birth.month=5;x.birth.day=13;y=x.birth.year;
② 代入,参照
例)
struct PERSON{char name[20];struct DATE_DATA{int year;int month;int day;
} birth;float height;float weight;
};
ピリオドを2回使う
プログラム例#include <stdio.h>#include <string.h>int main(void)
{struct S_ID{int ent_year;char dept;int id;
};
struct STUDENT_DATA{struct S_ID myid;char name[20];float height;float weight;
};struct STUDENT_DATA x;
x.myid.ent_year=2002;x.myid.dept=’L’;x.myid.id=110;strcpy(x.name,”Sakakibara”);x.height=180.0;x.weight=72.5;return 0;}
(左から続く)
構造体(5) 構造体の配列
① 宣言
struct 【構造体タグ名】 【構造体配列名】 [【要素数】];
② 参照,代入
【構造体配列名】[【要素番号】].【メンバ名】;
struct DATE_DATA{int year;int month;int day;
};
struct DATE_DATA x[10];
x[0].year=1990;y=x[3].month
プログラム例
#include <stdio.h>int main(void)
{struct S_ID{int ent_year;char dept;int id;
};
struct STUDENT_DATA{struct S_ID myid;char name[20];float height;float weight;
};
struct STUDENT_DATA x[100];
return 0;
}
(左から続く)
構造体変数の代入
#include <stdio.h>int main(void)
{struct S_ID{int ent_year;char dept;int id;
};
struct S_ID x,y;
x.ent_year = 2002;x.dept = ’L’ ;x.id = 110;y = x;
printf(”%d %c%d”,
y.ent_year,y.dept,y.id);return 0;}
構造体変数の場合,変数名での代入が可能!
注意!配列の場合,配列名での代入は不可!
top related