NekoC

猫でもわかるC言語の個人的なまとめ資料

View on GitHub

09_構造体.md

構造体

構文


struct 構造体タグ{
	メンバ;
	...;
};


struct data{
	char name[32];
	int age;
	double height;
	double weight;
};

struct 構造体タグ{
	メンバ,
	...,
} 構造体変数1, 構造体変数2,...;

struct {
	メンバ;
	...;
} 構造体変数1, 構造体変数2,...;

構造体の初期化

サンプルコード


#include <stdio.h>
#include <stdlib.h>

struct DATA{
	char name[32];
	int age;
	double height;
	double weight;
};

int main(){
	struct DATA me = { "Kousuke", 27, 176.0, 61.8 };
	printf("My name is %s, I'm %d years old, I'm %4.1f cm/%4.1f kg\n", me.name, me.age, me.height, me.weight);
	system("pause");
	return 0;
}

出力結果

My name is Kousuke, I'm 27 years old, I'm 176.0 cm/61.8 kg
続行するには何かキーを押してください . . .

構造体のコピー

サンプルコード


#include <stdio.h>
#include <stdlib.h>

int main(){
	struct DATA {
		int x;
		int y;
	} a = { 10, 20 }, b;

	/*copy struct*/
	b = a;

	printf("a's x = %d, a's y = %d\n"
		"b's x = %d, b's y = %d\n"
		, a.x, a.y, b.x, b.y);

	system("pause");
	return 0;
}

出力結果

a's x = 10, a's y = 20
b's x = 10, b's y = 20
続行するには何かキーを押してください . . .

構造体配列


#define STUDENTNO 5
#define SUBJECTNO 3

#include <stdio.h>
#include <stdlib.h>

int drawline(void);

struct RECORD{
	char name[32];
	int national;
	int mathematics;
	int english;
};

int main(){
	struct RECORD myClass[STUDENTNO] = {
		"田中", 80, 80, 55,
		"佐藤", 75, 90, 70,
		"太田", 50, 45, 30,
		"鈴木", 100, 55, 90,
		"加藤", 60, 85, 35,
	};
	int i, totalSum = 0, subjectSum[SUBJECTNO], studentSum[STUDENTNO];

	for (i = 0; i < SUBJECTNO; i++){
		subjectSum[i] = 0;
	}

	for (i = 0; i < STUDENTNO; i++){
		studentSum[i] = 0;
	}

	for (i = 0; i < STUDENTNO; i++){
		subjectSum[0] += myClass[i].national;
		subjectSum[1] += myClass[i].mathematics;
		subjectSum[2] += myClass[i].english;
	}

	for (i = 0; i < STUDENTNO; i++){
		studentSum[i] = myClass[i].national + myClass[i].mathematics + myClass[i].english;
	}

	for (i = 0; i < STUDENTNO; i++){
		totalSum += studentSum[i];
	}

	printf("**** 成績 ****\n\n");

	printf("%6s\t%6s\t%6s\t%6s\t%6s\n",
		"氏名", "国語", "数学", "英語", "合計");
	drawline();

	for (i = 0; i < STUDENTNO; i++){
		printf("%6s\t%6d\t%6d\t%6d\t%6d\n",
			myClass[i].name, myClass[i].national,
			myClass[i].mathematics, myClass[i].english, studentSum[i]);
	}
	drawline();

	printf("%6s\t%6.1f\t%6.1f\t%6.1f\t%6.1f\n",
		"平均",
		(double)subjectSum[0] / STUDENTNO,
		(double)subjectSum[1] / STUDENTNO,
		(double)subjectSum[2] / STUDENTNO,
		(double)totalSum / STUDENTNO);

	system("pause");
	return 0;
}

int drawline(){
	int i;

	for (i = 0; i < 35; i++){
		printf("-");
	}
	printf("\n");
	return 0;

}

出力結果


**** 成績 ****

  氏名    国語    数学    英語    合計
-----------------------------------
  田中      80      80      55     215
  佐藤      75      90      70     235
  太田      50      45      30     125
  鈴木     100      55      90     245
  加藤      60      85      35     180
-----------------------------------
  平均    73.0    71.0    56.0   200.0
続行するには何かキーを押してください . . .

構造体へのポインタ

アロー演算子

サンプルコード


#include <stdio.h>
#include <stdlib.h>

struct Data{
	char name[32];
	int age;
	double height;
	double weight;
	double bmi;
};

int struct_input(struct Data *);

int main(){
	struct Data MyData;
	char format[] = "%sさん(%d歳)のプロフィール\n"
		"身長 = %5.1fcm\t体重 = %5.1fkg\tBMI = %4.1f\n";
	char buffer[256];

	struct_input(&MyData);
	/*sprintf_s: 書式にのっとって変換した出力を文字列に格納する*/
	sprintf_s(buffer, format, MyData.name, MyData.age,
		MyData.height, MyData.weight, MyData.bmi);
	printf(buffer);

	/*ポインタによる参照*/
	struct Data *dataPointer = &MyData;
	printf("\n\n**** ポインタによる参照 *****\n");
	printf("name: %s\n", (*dataPointer).name);
	printf("age: %d\n", (*dataPointer).age);
	printf("height: %4.1f\n", (*dataPointer).height);
	printf("weight: %4.1f\n", (*dataPointer).weight);
	system("pause");
	return 0;
}

int struct_input(struct Data *p){
	printf("氏名---");
	gets_s(p->name);
	printf("年齢---");
	scanf_s("%d", &p->age);
	printf("身長(cm)---");
	scanf_s("%lf", &p->height);
	printf("体重(kg)---");
	scanf_s("%lf", &p->weight);
	p->bmi = p->weight * 10000.0 / (p->height * p->height);

	return 0;
}

出力結果


氏名---Kosuke Konishi
年齢---28
身長(cm)---176
体重(kg)---62
Kosuke Konishiさん(28歳)のプロフィール
身長 = 176.0cm  体重 =  62.0kg  BMI = 20.0


**** ポインタによる参照 *****
name: Kosuke Konishi
age: 28
height: 176.0
weight: 62.0
続行するには何かキーを押してください . . .