10 Structures and Others n n n Array

  • Slides: 28
Download presentation
10주차: Structures and Others

10주차: Structures and Others

목차 n n n Array와 Pointer의 관계 Call-by Reference continue & break 문 Storage

목차 n n n Array와 Pointer의 관계 Call-by Reference continue & break 문 Storage Class static String Library Structure Type 2

The Storage Class static n Storage classes n n auto, extern, register, static Static

The Storage Class static n Storage classes n n auto, extern, register, static Static n static n n n 자신이 선언된 block에서만 접근이 가능하지만 프로그램 이 실행되는 동안은 메모리에 계속 존재한다. auto의 경우는 block에서만 존재할 뿐더러 block에서만 메모리에 존재한다. extern static 12

extern static. . . static int v; int compute_sum(int a[]) { -변수 v는 extern

extern static. . . static int v; int compute_sum(int a[]) { -변수 v는 extern static 변수로 자신이 선언된 파일에서만 이용 이 가능하다. 나머지는 extern 변 수와 같다. static int call_cnt = 0; call_cnt++; . . . } 14

Structure Type의 선언 n 우선 타입을 선언하자. n n n 변수의 선언과는 다른 개념이다.

Structure Type의 선언 n 우선 타입을 선언하자. n n n 변수의 선언과는 다른 개념이다. struct student { char name[20]; int kor; int eng; int math; }; 위의 선언은 멤버 name[], kor, eng, math 를 가지는 student라는 struct type의 선언 이다. 17

struct student 타입은? struct student char name[20] int kor int eng int math 18

struct student 타입은? struct student char name[20] int kor int eng int math 18

struct Example(1/3) #include<stdio. h> struct student{ char name[20]; int kor, eng, math; }; int

struct Example(1/3) #include<stdio. h> struct student{ char name[20]; int kor, eng, math; }; int main(void) -struct student{. . 는 struct student라는 데이터 타입의 선 언이다. -struct student class_mem; 은 struct student 타입의 변수 class_mem의 선언이다. { struct student class_mem; . . . 19

struct Example(2/3) #include<stdio. h> #include<string. h>. . . int main(void) { -class_mem. kor는 class_mem이라는

struct Example(2/3) #include<stdio. h> #include<string. h>. . . int main(void) { -class_mem. kor는 class_mem이라는 변수의 멤 버 kor를 나타낸다. class_mem. kor는 int 타입이 다. struct student class_mem; class_mem. kor = 70; class_mem. eng = 60; . . . 20

struct Example(3/3) #include<stdio. h> #include<string. h>. . . int main(void) { struct student class_mem;

struct Example(3/3) #include<stdio. h> #include<string. h>. . . int main(void) { struct student class_mem; -strcpy(dst, src)는 string src 를 dst로 복사하는 함수이다. -이 예제에서는 변수 class_mem의 멤버 char name[20]에 “Babo”라는 값 을 복사한다. . . strcpy(class_mem. name, “Babo”); . . 21

strcpy 함수 n string. h 파일을 include하고 사용 n n #include<string. h> strcpy(char array(destination

strcpy 함수 n string. h 파일을 include하고 사용 n n #include<string. h> strcpy(char array(destination string), char array(source string)) 23

데이터 파일에서 항목들을 읽 어 오기(2/3). . int main(void) {. . struct student class_mem[10];

데이터 파일에서 항목들을 읽 어 오기(2/3). . int main(void) {. . struct student class_mem[10]; -class_mem이라는 struct student 타입의 배열을 선 언한다. -파일을 연다. char line[1024], tmp_name[20]; int tmpkor, tmpeng, tmpmath, i; FILE *dfile; dfile = fopen(“data. txt”, “r”); . . 25

데이터 파일에서 항목들을 읽 어 오기(3/3). . i = 0; while(fgets(line, 1024, dfile)){ sscanf(line,

데이터 파일에서 항목들을 읽 어 오기(3/3). . i = 0; while(fgets(line, 1024, dfile)){ sscanf(line, “%s %d %d %d”, tmp_name, &tmpkor, &tmpeng, & tmpmath); strcpy(class_mem[i]. name, tmp_name); class_mem[i]. kor = tmpkor; class_mem[i]. eng = tmpeng; class_mem[i]. math = tmpmath; i++; . . . } 26

fgets & sscanf 함수 n fgets(char array, char count, file pointer); n fgets(line, 1024,

fgets & sscanf 함수 n fgets(char array, char count, file pointer); n fgets(line, 1024, dfile); n n n dfile에서 한 줄을 읽어서(최대 1024 문자만큼) line에 복 사한다. 파일의 끝까지 읽었으면 NULL(0)을 return한다. sscanf(char array, “. . . ”, . . . ); n scanf와 같으나 단지 입력을 char array에서 받는 다. 27