Presentation is loading. Please wait.

Presentation is loading. Please wait.

10주차: Structures and Others

Similar presentations


Presentation on theme: "10주차: Structures and Others"— Presentation transcript:

1 10주차: Structures and Others

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

3 Array와 Pointer의 관계 int a[100];
위의 선언은 a[0], a[1], ..., a[99]의 정수 100개의 배열에 대한 선언이다. 위에서 a는 주소(pointer)이며 배열의 첫 번째 원소 a[0]의 주소를 나타낸다.

4 int a[100]은 메모리에 있다. a[0] a[1] a[99] 1000 1004 1396 a

5 a의 이용 int a[100]; int *iptr; iptr은 int pointer type 변수이다. iptr = a;
a의 값(1000번지)을 iptr에 넣는다. 즉, iptr은 1000번지라는 값을 가지게 된다. 그러면 *iptr은 무엇인가? iptr이 가리키는 주소는 a[0]의 주소이며 *iptr은 a[0]이다.

6 iptr은 1000이라는 값을 가지며 iptr이 a[0]을 가리킨다고 생각해도 좋다.
1004 1396 a a는 1000이라는 값을 가지는 포인터 상수이다. iptr iptr은 1000이라는 값을 가지며 iptr이 a[0]을 가리킨다고 생각해도 좋다.

7 iptr로 할 수 있는 일 *iptr는 a[0]이다. iptr + 1의 값은? a[0]의 다음 원소인 a[1]의 주소이다.
iptr은 int pointer이기 때문에 iptr + 1은 1001이 아니라 정수의 크기에 맞춰 계산된다(이 경우는 1004). 따라서 *(iptr + 1)은 a[1]이다.

8 Call-by-Reference 함수의 인자를 직접 접근하는 전달 방식 C에서는 call by value를 사용한다.
함수의 인자로 값을 복사해서 그대로 전달한다(6주차 강의 참고). call by reference의 효과는 포인터 변수를 이용한다.

9 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)를 넘겨 주는 것이므로 그 주소에 저장된 값을 변경할 수 있다. 포인터를 사용하지 않으면 함수의 인자로 받은 값을 변경할 수 없다.

10 continue & break 문 순환문(for, while, do-while) 내부에서 사용된다. continue;
continue를 만나면 순환문의 다음 순환(next iteration)으로 바로 넘어간다. break; 순환문을 빠져 나간다.

11 break 문 예제 -이 프로그램은 키보드에서 정수를 하나씩 입력 받다가 7을 입력 받으면 while loop을 빠져 나간다.
#include<stdio.h> int main(void) { ... while(scanf(“%d”, &i) == 1){ if(i == 7) break; } -이 프로그램은 키보드에서 정수를 하나씩 입력 받다가 7을 입력 받으면 while loop을 빠져 나간다.

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

13 static variable -call_cnt 변수는 static 변수이므로 compute_sum이 처음 호출될 때 메모리에 할당되고 그 이후 프로그램 종료시까지 계속 남아 있는다. -예제에서 call_cnt는 한 번만 0으로 초기화되고 그 이후는 계속 값이 더해진다. ... int compute_sum(int a[]) { static int call_cnt = 0; call_cnt++; }

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

15 extern & extern static 파일 1 파일 n 외부 영역(global)
static int v; /* 파일 n에서만 볼 수 있다. */ 함수 A 함수L main함수 외부 영역(global) int w; /* 모든 파일에서 볼 수 있다. */

16 String Library 프로그램의 첫 부분에 string.h를 include하고 사용하면 된다.
strcpy 함수 등이 있다.

17 Structure Type structure array와 같이 derived data type이다.
여러 가지 항목을 가진 객체를 변수로 만들고 싶을 때 사용한다. 예를 들어, 성적 처리 프로그램에서 학생 데이터

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

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

20 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의 선언이다.

21 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 타입이다.

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

23 struct 타입의 복사 struct student tclass_mem; tclass_mem = class_mem
class_mem의 각 멤버(name, kor, eng, math)들의 값이 tclass_mem의 각 값으로 복사된다.

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

25 데이터 파일에서 항목들을 읽어 오기(1/3) 노정렬 34 76 39 허준 45 23 89 전지현 12 34 29 ...

26 데이터 파일에서 항목들을 읽어 오기(2/3) -class_mem이라는 struct student 타입의 배열을 선언한다.
.... 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 타입의 배열을 선언한다. -파일을 연다.

27 데이터 파일에서 항목들을 읽어 오기(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++; ... }

28 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에서 받는다.

29 교재에서 강의와 연관된 부분 6장 9장 11장 p. 659(for fgets()) 6.3, 6.4, 6.11
9.1, 9.2, 9.3, 9.4 11장 11.3 p. 659(for fgets())

30 최종과제( ) 성적처리프로그램 데이터 파일의 형식 처리해야 할 내용 출력 양식 그 외의 요구 사항

31 데이터의 형식 학번 이름 국어 영어 수학 2000-001 노정렬 34 76 39 2000-002 허준 45 23 89
전지현 .... 데이터 파일은 강의 홈페이지에서 가져갈 것. 총 50명의 데이터임

32 처리해야 할 내용 각 학생의 평균 점수 계산 각 과목별 평균 점수 계산 전체 평균의 계산
학생별 평균의 평균 각 학생의 평균 점수순으로 정렬하여 화면에 출력

33 출력 양식 학번 이름 국어 영어 수학 평균 ....(평균점수순으로 정렬하여 출력) 과목별 평균 전체 평균

34 그 외의 요구 사항 학생들을 점수별로 정렬하는 작업은 함수를 이용할 것
예) void sort_student(struct student[], int arrcnt); 과제에 어려움이 있으면 이나 직접 찾아와서 질문 하세요...(301동 451호, 오전 10시 – 오후 9시 정도까지 있습니다.)

35 제출물 원래 숙제에서 제출하던 것 소스 코드는 e-mail로도 보낼 것 표지, 설명, 소스코드, 실행결과, 감상

36 기말 고사 6월 20일 예정 강의 자료를 중심으로 공부할 것


Download ppt "10주차: Structures and Others"

Similar presentations


Ads by Google