Post on 04-Jan-2016
description
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]
10001004
1396
a
a 는 1000 이라는 값을 가지는 포인터 상수이다 .
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 과 배열 a[100]
a[0]a[1]
a[99]
10001004
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 문 예제
#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
...
int compute_sum(int a[])
{
static int call_cnt = 0;
call_cnt++;
...
}
-call_cnt 변수는 static 변수이므로 compute_sum이 처음 호출될 때 메모리에 할당되고 그 이후 프로그램 종료시까지 계속 남아 있는다 .
- 예제에서 call_cnt 는 한 번만 0 으로 초기화되고 그 이후는 계속 값이 더해진다 .
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
외부 영역 (global)
함수 A
main 함수
함수 L
외부 영역(global)
int w; /* 모든 파일에서 볼 수 있다 . */
static int v; /* 파일 n 에서만 볼 수 있다 . */
파일 1 파일 n
16
Structure Type structure
array 와 같이 derived data type 이다 . 여러 가지 항목을 가진 객체를 변수로
만들고 싶을 때 사용한다 . 예를 들어 , 성적 처리 프로그램에서 학생
데이터
17
Structure Type 의 선언 우선 타입을 선언하자 .
변수의 선언과는 다른 개념이다 . struct student { char name[20]; int
kor; int eng; int math;}; 위의 선언은 멤버 name[], kor, eng,
math 를 가지는 student 라는 struct type 의 선언이다 .
18
struct student 타입은 ?
char name[20]
int kor
int eng
int math
structstudent
19
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 의 선언이다 .
20
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 타입이다 .
21
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” 라는 값을 복사한다 .
22
struct 타입의 복사 struct student tclass_mem; tclass_mem = class_mem
class_mem 의 각 멤버 (name, kor, eng, math) 들의 값이 tclass_mem 의 각 값으로 복사된다 .
직접 하나씩 따로따로 복사해도 된다 . 그러나 , 이 경우 손만 아플 뿐이다 . 손보다는 손가락이겠지…썰렁 -.-;
23
strcpy 함수 string.h 파일을 include 하고 사용
#include<string.h> strcpy(char array(destination
string), char array(source string))
24
데이터 파일에서 항목들을 읽어 오기 (1/3)
엄재롱 34 76 39허준 45 23 89전지현 12 34 29전도환 0 10 60김엉삼 5 10 42노대우 40 20 15...
데이터 파일
25
데이터 파일에서 항목들을 읽어 오기 (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 타입의 배열을 선언한다 .
- 파일을 연다 .
26
데이터 파일에서 항목들을 읽어 오기 (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++;
...
}
27
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 에서 받는다 .
28
교재에서 강의와 연관된 부분 6 장
6.3, 6.4, 6.11 9 장
9.1, 9.2, 9.3, 9.4 11 장
11.3 p. 659(for fgets())