include stdio h int mainint argc char argv







![引数を出力するプログラム #include <stdio. h> int main(int argc, char *argv[]) { int i; for (i=0; 引数を出力するプログラム #include <stdio. h> int main(int argc, char *argv[]) { int i; for (i=0;](https://slidetodoc.com/presentation_image_h2/c60b577d0edd1cb97a13483a2f043dbd/image-8.jpg)
















![typedefを用いる typedef struct student{ int id; int score; } STUDENT; STUDENT students[5]; typedefを用いる typedef struct student{ int id; int score; } STUDENT; STUDENT students[5];](https://slidetodoc.com/presentation_image_h2/c60b577d0edd1cb97a13483a2f043dbd/image-25.jpg)


![構造体と関数 #include <stdio. h> struct student{ char name[20]; int math; int jlang; }; void 構造体と関数 #include <stdio. h> struct student{ char name[20]; int math; int jlang; }; void](https://slidetodoc.com/presentation_image_h2/c60b577d0edd1cb97a13483a2f043dbd/image-28.jpg)



- Slides: 31
引数を出力するプログラム #include <stdio. h> int main(int argc, char *argv[]) { int i; for (i=0; i < argc; i++){ printf(“t%sn”, argv[i]); } return 0; }
ポインタ変数の操作(1) int i = 10; int j = 20; int *ptr = &i printf(“i=%dn”, &i) printf(“ptr=%dn”, ptr) printf(“i=%dn”, i) printf(“*ptr=%dn”, *ptr) ptrはiをさすポインタ変数 アドレス 108 i 10 104 j 20 100 ptr 108 0 変数とアドレス
ポインタと関数(1) #include <stdio. h> void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } int main(){ int a = 5; int b = 3; swap (a, b); printf(“a=%dn”, a); printf(“b=%dn”, b); return 0; } int型の変数を入れ替えるプログラム
ポインタと関数(2) #include <stdio. h> void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } int main(){ int a = 5; int b = 3; swap (a, b); printf(“a=%dn”, a); printf(“b=%dn”, b); return 0; } アドレス 108 a 104 b 94 90 0 交換 x 5 y 3 変数とアドレス
ポインタと関数(3) #include <stdio. h> void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int main(){ int a = 5; int b = 3; swap (&a, &b); printf(“a=%dn”, a); printf(“b=%dn”, b); return 0; } int型の変数を入れ替えるプログラム 108 a *x 104 b *y 交換 94 x 108 90 y 104 0 変数とアドレス
構造体(structure)(使用例1) int main() 構造体の定義、 { 構造体変数の宣言 int i; struct student{ 構造体変数の初期化 int id; を同時に行う場合 int score; } students[5] = { {70001234, 80}, {70204578, 70}, {70157384, 60}, {70355678, 75}, {70207658, 49} }; for(i=0; i<5; i++){ printf("student id: %d, score: %dn", students[i]. id, students[i]. score); } }
構造体(structure)(使用例2) struct student{ int id; int score; }; 構造体の定義を分け た場合 変数の宣言、初期化 は同時に行っている Int main() { int i; struct students[5] = { {70001234, 80}, {70204578, 70}, {70157384, 60}, {70355678, 75}, {70207658, 49} }; for(i=0; i<5; i++){ printf("student id: %d, score: %dn", students[i]. id, students[i]. score); } }
構造体(structure)(使用例3) struct student{ int id; int score; }; 構造体の定義を分け た場合 初期化は行わずに 代入を行っている。 Int main() { int i; struct students[5]; for(i=0; i<5; i++){ students[i]. id = i; students[i]. score = i; } for(i=0; i<5; i++){ printf("student id: %d, score: %dn", students[i]. id, students[i]. score); } }
typedefを用いる typedef struct student{ int id; int score; } STUDENT; STUDENT students[5];
【補足2】構造体ポインタ struct student st; struct student *sp; sp = &st; sp->id = 7000123; (*sp). score = 23; sp st id score 7000123 23 printf(“%dn”, sp->score);
練習 1:構造体の練習 以下の情報をキーボードから入力し、後でまとめて出力する。 学籍番号 ログイン名 名前 %. /a. out 80749423 kazuhisa “kazuhisa matsuzono” Number: 7045678 Login name: kazuhisa Name: kazuhisa matsuzono
構造体と関数 #include <stdio. h> struct student{ char name[20]; int math; int jlang; }; void clear(struct student *sei); int main(){ struct student seito; /* clearing data in seito */ clear(&seito); return 0; } void clear(struct student *sei){ strcpy (sei->name, “”); sei->math=0; sei->jlang=0; }
課題 Determine size of struct student Determine size of array of struct student Use memcpy() to copy struct Use assignment to copy struct Use memset() to clear memory