280 likes | 412 Views
10주차: Structures and Others. 목차. Array 와 Pointer 의 관계 Call-by Reference continue & break 문 Storage Class static String Library Structure Type. Array 와 Pointer 의 관계. int a[100]; 위의 선언은 a[0], a[1], ..., a[99] 의 정수 100개의 배열에 대한 선언이다.
E N D
목차 • Array와 Pointer의 관계 • Call-by Reference • continue & break 문 • Storage Class static • String Library • Structure Type
Array와 Pointer의 관계 • int a[100]; • 위의 선언은 a[0], a[1], ..., a[99]의 정수 100개의 배열에 대한 선언이다. • 위에서 a는 주소(pointer)이며 배열의 첫 번째 원소 a[0]의 주소를 나타낸다.
1000 a[0] 1004 a[1] a a는 1000이라는 값을 가지는 포인터 상수이다. 1396 a[99] int a[100]은 메모리에 있다.
a의 이용 • int a[100]; int *iptr; • iptr은 int pointer type 변수이다. • iptr = a; • a의 값(1000번지)을 iptr에 넣는다. 즉, iptr은 1000번지라는 값을 가지게 된다. • 그러면 *iptr은 무엇인가? iptr이 가리키는 주소는 a[0]의 주소이며 *iptr은 a[0]이다.
iptr 1000 a[0] iptr은 1000이라는 값을 가지며 iptr이 a[0]을 가리킨다고 생각해도 좋다. 1004 a[1] a a는 1000이라는 값을 가지는 포인터 상수이다. 1396 a[99] 포인터 iptr과 배열 a[100]
iptr로 할 수 있는 일 • *iptr는 a[0]이다. • iptr + 1의 값은? • a[0]의 다음 원소인 a[1]의 주소이다. • iptr은 int pointer이기 때문에 iptr + 1은 1001이 아니라 정수의 크기에 맞춰 계산된다(이 경우는 1004). • 따라서 *(iptr + 1)은 a[1]이다.
Call-by-Reference • 함수의 인자를 직접 접근하는 전달 방식 • C에서는 call by value를 사용한다. • 함수의 인자로 값을 복사해서 그대로 전달한다(6주차 강의 참고). • call by reference의 효과는 포인터 변수를 이용한다.
Call-by-Reference Example #include<stdio.h> void swap(int *p, int *q) { int tmp; tmp = *p; *p = *q; *q = tmp; } int main(void) { int i = 3, j = 5; swap(&i, &j); .... • 포인터를 함수의 인자로 넘겨주는 것은 어떤 변수의 주소(&i, &j)를 넘겨 주는 것이므로 그 주소에 저장된 값을 변경할 수 있다. • 포인터를 사용하지 않으면 함수의 인자로 받은 값을 변경할 수 없다.
continue & break 문 • 순환문(for, while, do-while) 내부에서 사용된다. • continue; • continue를 만나면 순환문의 다음 순환(next iteration)으로 바로 넘어간다. • break; • 순환문을 빠져 나간다.
break 문 예제 #include<stdio.h> int main(void) { ... while(scanf(“%d”, &i) == 1){ if(i == 7) break; } ... -이 프로그램은 키보드에서 정수를 하나씩 입력 받다가 7을 입력 받으면 while loop을 빠져 나간다.
The Storage Class static • Storage classes • auto, extern, register, static • Static • static • 자신이 선언된 block에서만 접근이 가능하지만 프로그램이 실행되는 동안은 메모리에 계속 존재한다. • auto의 경우는 block에서만 존재할 뿐더러 block에서만 메모리에 존재한다. • extern static
static variable -call_cnt 변수는 static 변수이므로 compute_sum이 처음 호출될 때 메모리에 할당되고 그 이후 프로그램 종료시까지 계속 남아 있는다. -예제에서 call_cnt는 한 번만 0으로 초기화되고 그 이후는 계속 값이 더해진다. ... int compute_sum(int a[]) { static int call_cnt = 0; call_cnt++; ... }
extern static ... static int v; int compute_sum(int a[]) { static int call_cnt = 0; call_cnt++; ... } -변수 v는 extern static 변수로 자신이 선언된 파일에서만 이용이 가능하다. 나머지는 extern 변수와 같다.
extern & extern static 파일 1 파일 n 외부 영역(global) static int v; /* 파일 n에서만 볼 수 있다. */ 함수 A 함수L main함수 외부 영역(global) int w; /* 모든 파일에서 볼 수 있다. */
Structure Type • structure • array와 같이 derived data type이다. • 여러 가지 항목을 가진 객체를 변수로 만들고 싶을 때 사용한다. • 예를 들어, 성적 처리 프로그램에서 학생 데이터
Structure Type의 선언 • 우선 타입을 선언하자. • 변수의 선언과는 다른 개념이다. • struct student { char name[20]; int kor; int eng; int math;}; • 위의 선언은 멤버 name[], kor, eng, math를 가지는 student라는 struct type의 선언이다.
struct student char name[20] int kor int eng int math struct student 타입은?
struct Example(1/3) #include<stdio.h> struct student{ char name[20]; int kor, eng, math; }; int main(void) { struct student class_mem; ... -struct student{ ....는 struct student라는 데이터 타입의 선언이다. -struct student class_mem;은 struct student 타입의 변수 class_mem의 선언이다.
struct Example(2/3) #include<stdio.h> #include<string.h> ... int main(void) { struct student class_mem; class_mem.kor = 70; class_mem.eng = 60; ... -class_mem.kor는 class_mem이라는 변수의 멤버 kor를 나타낸다. class_mem.kor는 int 타입이다.
struct Example(3/3) #include<stdio.h> #include<string.h> ... int main(void) { struct student class_mem; .... strcpy(class_mem.name, “Babo”); .... • strcpy(dst, src)는 string src를 dst로 복사하는 함수이다. • 이 예제에서는 변수 class_mem의 멤버 char name[20]에 “Babo”라는 값을 복사한다.
struct 타입의 복사 • struct student tclass_mem; • tclass_mem = class_mem • class_mem의 각 멤버(name, kor, eng, math)들의 값이 tclass_mem의 각 값으로 복사된다. • 직접 하나씩 따로따로 복사해도 된다. • 그러나, 이 경우 손만 아플 뿐이다. • 손보다는 손가락이겠지…썰렁 -.-;
strcpy 함수 • string.h 파일을 include하고 사용 • #include<string.h> • strcpy(char array(destination string), char array(source string))
데이터 파일에서 항목들을 읽어 오기(1/3) 엄재롱 34 76 39 허준 45 23 89 전지현 12 34 29 전도환 0 10 60 김엉삼 5 10 42 노대우 40 20 15 ... 데이터 파일
데이터 파일에서 항목들을 읽어 오기(2/3) .... int main(void) { .... struct student class_mem[10]; char line[1024], tmp_name[20]; int tmpkor, tmpeng, tmpmath, i; FILE *dfile; dfile = fopen(“data.txt”, “r”); .... -class_mem이라는 struct student 타입의 배열을 선언한다. -파일을 연다.
데이터 파일에서 항목들을 읽어 오기(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++; ... }
fgets & sscanf 함수 • fgets(char array, char count, file pointer); • fgets(line, 1024, dfile); • dfile에서 한 줄을 읽어서(최대 1024 문자만큼) line에 복사한다. • 파일의 끝까지 읽었으면 NULL(0)을 return한다. • sscanf(char array, “...”, ...); • scanf와 같으나 단지 입력을 char array에서 받는다.
교재에서 강의와 연관된 부분 • 6장 • 6.3, 6.4, 6.11 • 9장 • 9.1, 9.2, 9.3, 9.4 • 11장 • 11.3 • p. 659(for fgets())