Presentation is loading. Please wait.

Presentation is loading. Please wait.

ネットワークプログラミング 第4回「C言語の基礎~ポインタと配列」

Similar presentations


Presentation on theme: "ネットワークプログラミング 第4回「C言語の基礎~ポインタと配列」"— Presentation transcript:

1 ネットワークプログラミング 第4回「C言語の基礎~ポインタと配列」
2009年秋学期 Rodney Van Meter

2 授業Webページ SFC-SFShttps://vu.sfc.keio.ac.jp/sfc- sfs/ 課題・授業資料などの情報を掲示します
本講義を「MY時間割(仮)」へ登録してください 課題・授業資料などの情報を掲示します 課題は毎回こちらに提出!! 今日の課題締め切り 10/27(月)23:59分まで! 遅れて提出する方は要連絡

3 今期の授業スケジュール(予定) 第1回:イントロダクション 第2回:C言語の基礎~関数・変数・Makefile
第5回:ネットワークとプログラミング(1) (10/27)‏ 第6回:ネットワークとプログラミング(2) ()‏ 第7回:ネットワークプログラミング実践(1) ()‏ 第8回:ネットワークプログラミング実践(2) ()‏ 第9回:応用ネットワークプログラミング(1) ()‏ 第10回:応用ネットワークプログラミング(2) ()‏ 第11回:ミニプロ ()‏

4 今日のお題 ポインタ ポインタの基礎(復習)‏ ポインタと配列 ポインタと文字列 リスト構造 構造体の復習 演習

5 ポインタ復習

6 ポインタとは? ポインタ変数 int i ; int j ; int *ptr *の意味 「変数」を指す変数
アドレス ポインタ変数 「変数」を指す変数 int i ; int j ; int *ptr *の意味 「intへのポインタ型」であると宣言   するための指定子 108 i 104 j 100 ptr 変数とアドレス

7 ポインタ変数の操作(1)‏ int i = 10; int j = 20; int *ptr = &i;
アドレス int i = 10; int j = 20; int *ptr = &i; printf(“&i=%d\n”, &i)‏ printf(“ptr=%d\n”, ptr)‏ printf(“i=%d\n”, i)‏ printf(“*ptr=%d\n”,*ptr)‏ ptrはiをさすポインタ変数 108 i 10 104 j 20 100 ptr 108 変数とアドレス

8 ポインタ変数の操作(2)‏ int x=1, y=5; int z[10]; int *p p=&x; /* pはxを指す */
y=*p; /* yに1を代入 */ *p = 0; /* xが0になる */ p=&z[2]; /* pはz[2]を指す */

9 ポインタと配列の関係(1/3)‏ int a[7] = {6,5,4,3,2,1,0}; int *ptr; ptr = &a[0];
printf(“a[0]=%d\n”, a[0]); printf(“*ptr=%d\n”, *ptr); a[1] a[2] a[3] a[4] a[5] a[6] ptr &a[0]

10 ポインタと配列の関係(2/3)‏ for (i=0;i<7; i ++){ printf(“a[%d]=%d\n”,
i, a[i]); printf(“*(ptr+%d)=%d\n”, *(ptr+i)); } a[0]と*ptrは同じ実体をさす a[1]と*(ptr+1)は同じ実体をさす  ・・・・ a[6]と*(ptr+6)は同じ実体をさす a[0] *ptr a[1] *(ptr+1)‏ a[2] *(ptr+2)‏ a[3] *(ptr+3)‏ a[4] *(ptr+4)‏ a[5] *(ptr+5)‏ a[6] *(ptr+6)‏ ptr &a[0]

11 ポインタと配列の関係(3/3)‏ C言語の規則 配列a[n]に対して,a[i]を*(a+i)と書ける &a[i]をa+iと書ける
ポインタpに対して,*(p+i)をp[i]と書ける  ptr[3] ptr[2] ptr[1] ptr[0] *(ptr+3)‏ *(ptr+2)‏ *(ptr+1)‏ *ptr *(a+3)‏ *(a+2)‏ *(a+1)‏ *a a[3] a[2] a[1] a[0] &ptr[3] &ptr[2] &ptr[1] &ptr[0] ptr+3 ptr+2 ptr+1 ptr a+3 a+2 a+1 a &a[3] &a[2] &a[1] &a[0]

12 ポインタと文字列

13 ポインタと文字列 メモリ空間 string[] "T" "h" char string[] = "This is test."; "i"
アドレス string[] 100 "T" 101 "h" char string[] = "This is test."; 102 "i" "で括った文字列は どこかに格納されている 103 "s" 104 " " 105 "i" 106 "s" char *s; 文字(列)が入っている変数        のアドレスが入る変数 107 " " 108 "t" 109 "e" 110 "s" 111 "t" 112 "." char *s = string; 113 NULL 200 100 201 ….

14 文字配列と文字定数とポインタ 文字列定数 宣言時にどこかに存在する(無名配列)‏ h e l l o \0
文字列定数 宣言時にどこかに存在する(無名配列)‏ h e l l o \0 “hello” どこかに存在する 文字配列  char str1[8] = “hello”; str1 8つの文字変数からなる str1という配列を用意 str1 配列に対して「どこかに 存在している」”hello”の 内容をコピー(初期化)‏ h e l l o \0 ポインタ  char *ptr = “hello”; ptr 文字変数を指すポインタ型 変数ptrを用意 ptr ポインタ変数に対して 「どこかに存在している」 ”hello”のアドレスをコピー(初期化)‏ 0xff0120

15 ポインタと文字列 h e l l o \0 h e l l o \0 char mystr[8] = “hello”;
char *ptr = “hello”; h e l l o \0 mystr[0] mystr[1] mystr[2] mystr[3] mystr[4] ptr = “hello” ptr + 1 = “ello” ptr + 2 = “llo” ptr + 3 = “lo” ptr + 4 = “o” h e l l o \0 *ptr ptr[0] *(ptr+1)‏ ptr[1] *(ptr+2)‏ ptr[2] *(ptr+3)‏ ptr[3] *(ptr+4)‏ ptr[4]

16 練習問題 文字列をコピーする関数 void strcpy(char *s, char *t)を 作成せよ
#include <stdio.h> void copy(char *dst, char *src, int maxlen){ /* ここに記述する */ } int main(){ char name[]= “your name”; char cp[64]; copy(cp, name, 64); printf(“cp:\t%s\n”,cp); /* cpにnameがcopyされている                  ことを確認 */ return 0;

17 リスト構造

18 構造体(structure)‏ 構造体を用いると、複数のデータを1つのまとまりと して取り扱うことができる
Ex: 1人の学生に対して学籍番号と成績 struct student{ int id; int score; }; 異なる型も混在できる #define MAXLEN 32 struct student2{ char name[MAXLEN];

19 構造体の定義 struct student{ int id; int score; }; 定義の方法 使用例 struct 構造体名{
型 メンバ名1; 型 メンバ名2; }; struct student{ int id; int score; };

20 構造体を用いた変数 定義した構造体を用いて変数はプリミティブな 型と同じ方法で宣言できる int a; struct student b;

21 構造体メンバへのアクセス(1)‏ 構造体メンバへのアクセスは.(ドット)演算子を 用いる
Ex: struct studentの変数bの点数(メンバ 名:score)を表示する struct student b = { ,70}; printf(“bの点数は%dです\n”, b.score);

22 構造体メンバへのアクセス(2)‏ 構造体へのポインタの変数のメンバへのアクセスは ->演算子を用いる
構造体へのポインタの変数のメンバへのアクセスは ->演算子を用いる Ex: struct studentの変数bの点数(メンバ名:score)を表 示する struct student b = { ,70}; struct student *c = &b; printf(“bの点数は%dです\n”, c->score); 構造体へのポインタ変数->メンバ名 は (*構造体へのポインタ変数).メンバ名 と同意

23 構造体(structure)(使用例3)‏
struct student{ int id; int score; }; int main()‏ { int i; struct student 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:%d\n", students[i].id, students[i].score); 構造体の定義を分け た場合 初期化は行わずに 代入を行っている。

24 【補足】構造体ポインタ struct student st; struct student *sp; sp = &st;
sp->id = ; (*sp).score = 23; sp st id score 23 printf(“%d\n”, sp->score);

25 リスト構造 次の領域を保持することで、記憶領域を連結す る構造。 こんなとき便利 まとまったデータを扱う 並びかえ
Can use like stack: push & pop 構造体の要素 *top

26 連結リスト(linked list):宣言
構造体の中に次の構造体へのポインタを格納 struct student{ int id; int score; struct student *next; }

27 連結リスト:メモリの確保 何人のリストが事前にわからないので、いくつ宣言すれば いいかわからない。
→ 最初のノードだけ宣言して、あとはそのつど領域を確保 struct student *top; struct student *current current = (struct student *)malloc(sizeof(struct student)); top = current;

28 【補足】動的なメモリ空間の割り当て char *cp; struct student *sp; (1)
cp = (char *)malloc(64); sp = (struct student *)malloc(64); (2) cp = (char *)malloc(sizeof(ch)); sp = (struct student *)malloc(sizeof(struct student)*10);  →struct student sp[10]と同様 sp st id score 23 キャスティング サイズ 23 23 23 ・・・

29 連結リスト:メンバーの情報を格納 mallocで領域を確保した後は、メンバを追加
current->id = atoi(buf_id); current->score = atoi(buf_score); current->next = NULL;

30 練習: リスト構造を使おう 学籍番号,名前をキーボードから入力し,リスト構造に 順次格納する.最後にまとめて出力せよ. ・実行例 001
% ./a.out 001 kazuhisa 002 three 003 haeena finish 001 kazuhisa 002 three 003 haeena Kazuhisa *next 002 three 入力 *next 003 haeena *next 出力

31 課題 Add a “next” pointer to your struct student.
Modify your program to malloc(struct student) for 30 students, one at a time. Store the malloc()ed memory address in the “next” of each one. Print out the student ids and scores, same as before. Use free() to release the memory.

32 課題 Modify your program to malloc(struct student) repeatedly until it fails (or 100,00,000 times if you are on a shared server). Use perror() to see what the failure is. Use printf(); after every 10,000 mallocs to see how fast the program runs. Does it get slower over time? Use your system monitoring tools to see how large the process grows. Use free() to release the memory.


Download ppt "ネットワークプログラミング 第4回「C言語の基礎~ポインタと配列」"

Similar presentations


Ads by Google