struct student st struct student sp sp st

  • Slides: 23
Download presentation

構造体ポインタ struct student st; struct student *sp; sp = &st; sp->id = 7000123; (*sp).

構造体ポインタ struct student st; struct student *sp; sp = &st; sp->id = 7000123; (*sp). score = 23; sp st id score 7000123 23 printf(“%dn”, sp->score);

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

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

【補足】動的なメモリ空間の割り当て char *cp; struct student *sp; sp st (1) cp = (char *)malloc(64); 7000123

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

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

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

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

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

例 struct student *current, *top, *next; char buf[1024] top = NULL; while(1){ memset(buf, 0,

例 struct student *current, *top, *next; char buf[1024] top = NULL; while(1){ memset(buf, 0, 1024) printf(“input your number or ‘finish’n>>”); fgets(buf, 1024, stdin); if(strncmp(buf, “finish”, 6) == 0){ break; } printf(“input your namen>> “); fgets(&buf[strlen(buf)], 1024, stdin); if( top == NULL){ current = (struct student *)malloc(sizeof(struct student)); top = current; }else{ current->next = (struct student *)malloc(sizeof(struct student)); current = current->next; } strcpy(current->buf, buf); current->next = NULL; }

open()/read()/write()/close()の例 #include <fcntl. h> #include <sys/types. h> #include <sys/uio. h> #include <unistd. h> #define

open()/read()/write()/close()の例 #include <fcntl. h> #include <sys/types. h> #include <sys/uio. h> #include <unistd. h> #define BUFSIZE 1024 int main() { char buf[BUFSIZE]; int fd; int nbyte; fd = open(“test. txt”, O_RDONLY, 0); while((nbyte = read(fd, buf, BUFSIZE)) > 0) { write(1, buf, nbyte); } close(fd); exit(0); }

ファイルを扱う関数 o fopen(char *filename, char *mode) n o o o r, w, a, r+,

ファイルを扱う関数 o fopen(char *filename, char *mode) n o o o r, w, a, r+, w+, a+ fgets(char *s, int length, FILE *fd) fgetc(FILE *fd) fclose(FILE *fd) 次のページに練習問題あり

練習問題 #include <stdio. h> int main(int argc, char *argv[]) { FILE *fp; char buf[1024];

練習問題 #include <stdio. h> int main(int argc, char *argv[]) { FILE *fp; char buf[1024]; int c; fp = fopen(argv[1], "r"); while((fgets(buf, sizeof(buf), fp)) != NULL){ fputs(buf, stdout); } fclose(fp); exit(0); }