1 / 31

Network Programming in C ネットワークプログラミング Lecture 4: Pointers & Arrays 第 4 回「 C 言語の基礎~ポインタと配列」

20 10 Fall Semester 2010 年秋学期 Rodney Van Meter. Network Programming in C ネットワークプログラミング Lecture 4: Pointers & Arrays 第 4 回「 C 言語の基礎~ポインタと配列」. 授業 Web ページ. SFC-SFShttps://vu.sfc.keio.ac.jp/sfc-sfs/ 本講義を「 MY 時間割(仮)」へ登録してください 課題・授業資料などの情報を掲示します 課題は毎回こちらに提出 !! 今日の課題締め切り 10/27( 月 )23:59 分まで!

Download Presentation

Network Programming in C ネットワークプログラミング Lecture 4: Pointers & Arrays 第 4 回「 C 言語の基礎~ポインタと配列」

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2010 Fall Semester 2010 年秋学期 Rodney Van Meter Network Programming in CネットワークプログラミングLecture 4: Pointers & Arrays第4回「C言語の基礎~ポインタと配列」

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

  3. 今期の授業スケジュール(予定) 第1回 9/28: Introduction / イントロダクション 第2回 10/5:C Basics~Functions, Variables, Data Types・Makefiles 第3回 10/12:C Basics~Command Line Arguments・ Structures ・ Pointers 第4回 10/19:C Basics~Pointers & Arrays ・ Lists 第5回 10/26: file I/O・ Network Programming 第6回 11/2: Network Programming (1)‏ 第7回 11/9: Network Programming (2)‏ 第8回 11/16: Network Programming (3) 第9回 11/30: Applied Network Programming (1) 第10回 12/7: Applied Network Programming (2) 第11回 12/14: Work on Projects 第12回 12/21: Work on Projects 第13回 1/19: Final Presentations!!!

  4. Today’s Topics More on Pointers Review of basics‏ Pointers and arrays Pointers and character arrays Incrementing pointers List structures

  5. Pointer Practice

  6. What’s a Pointer? A pointer is a variable It points to another variable int i = 10; int j = 20; int *ptr meaning of * in declaration, says this varis a pointer to the named type Address i 108 104 j ptr 100 0 Variables & addresses

  7. Pointer Exercise 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 points to variable i アドレス i 108 10 104 j 20 ptr 108 100 0 変数とアドレス

  8. int x=1, y=5; int z[10]; int *p; p=&x; /* p points to x*/ y=*p;/*y gets 1 */ *p = 0; /* x becomes 0*/ p=&z[2]; /* p points to z[2] */ More on Pointers

  9. Pointers & Arrays (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[0] *ptr a[1] a[2] a[3] a[4] a[5] a[6] ptr &a[0]

  10. Pointers & Arrays (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] and *ptr point to same place a[1] and *(ptr+1) point to same place  ・・・・ a[6] and *(ptr+6) point to same place a[0] *ptr a[1] *(ptr+1)‏ a[2] *(ptr+2)‏ a[3] *(ptr+3)‏ a[4] *(ptr+4)‏ *(ptr+5)‏ a[5] *(ptr+6)‏ a[6] ptr &a[0]

  11. Pointers & Arrays (3/3)‏ a[0] *a *ptr ptr[0] a[1] *(a+1)‏ *(ptr+1)‏ ptr[1] a[2] *(a+2)‏ *(ptr+2)‏ ptr[2] a[3] *(a+3)‏ *(ptr+3)‏ ptr[3] &a[0] a ptr &ptr[0] &a[1] a+1 ptr+1 &ptr[1] &a[2] a+2 ptr+2 &ptr[2] &a[3] a+3 ptr+3 &ptr[3] • C expressions • For a[i], can write *(a+i) • For &a[i], can write a+i • Compiler treats them the same

  12. Pointers & Character Arrays (Strings)

  13. Pointers & Character Arrays Memory Space アドレス string[] 100 "T" 101 "h" char string[] = "This is test."; 102 "i" Allocates and initializesarray, including NULL. 103 "s" 104 " " 105 "i" 106 "s" char *s; holds address of memory holding array 107 " " 108 "t" 109 "e" 110 "s" 111 "t" char *s = string; 112 "." 113 NULL 200 100 201 ….

  14. 文字配列と文字定数とポインタ 文字列定数 宣言時にどこかに存在する(無名配列)‏ 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. Pointers into strings 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+3)‏ ptr[3] *(ptr+4)‏ ptr[4] *(ptr+1)‏ ptr[1] *(ptr+2)‏ ptr[2] *ptr ptr[0]

  16. Practice void strcpy(char *s, char *t) is no good. Let’s make a better one. #include <stdio.h> #define STRLEN 64 int copy(char *dst, char *src, int maxlen){ /* you write it: return 0 if successful, 1 if truncated*/ } int main(){ char name[]= “your name”; char cp[STRLEN]; int retval; retval = copy(cp, name, sizeof(cp)); /* check and print retval: did copy succeed? */ printf(“cp:\t%s\n”,cp); return 0; }

  17. List Structuresリスト構造

  18. 構造体(structure)‏ A structure can hold more than one piece of data, of the same or different types Ex: Holding ID & test score for a student struct student{ int id; int score; }; Different element types: struct student2{ char name[32]; int score; };

  19. Defining a Structure struct structname{ type member1; type member2; : : }; Example • Form struct student{ int id; int score; };

  20. Using structs as variables After defining struct, can use when defining variables Instead of type, use struct name int a; struct student b;

  21. Accessing struct members (1)‏ Simplest way is using . (ドット)演算子 Ex: struct student’s score value:struct student b = {70000000,70}; printf(“bの点数は%dです\n”, b.score);

  22. Accessing struct members (2)‏ Can also use a pointer to a struct: ->演算子 Ex: score in struct studentstruct student b = {70000000,70}; struct student *c = &b; printf(“bの点数は%dです\n”, c->score); In place of -> can also use(*pointer).member(*c).score

  23. Initialize at run time instead of compile time Using structures: example 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. Pointers to structs struct student st; struct student *sp; sp = &st; sp->id = 7000123; (*sp).score = 23; sp st 7000123 id score 23 printf(“%d\n”, sp->score);

  25. Linked List Structure (連結リスト) Connects one instance of a struct to another Ordered list Can use like stack: push & pop Can be allocated dynamically(when data amount not known at compile time) pointer to next *top struct main body

  26. Defining Linked Lists Add a pointer to the next struct(by convention, called “next”, and usually last in the struct) struct student{ int id; int score; struct student *next; }

  27. Allocating Memory for Lists連結リスト:メモリの確保 Since we don’t know at compile time how many students there are, need to do it dynamically. →Start with just a pointer, then ask system to give you memory using malloc 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 7000123 id 7000123 score 23 7000123 サイズ 23 7000123 キャスティング 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 .  single dot ends input 001 kazuhisa 002 three 003 haeena Kazuhisa 002 *next three 入力 *next 003 haeena *next 出力

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

More Related