SlideShare a Scribd company logo
고급변수 사용
포인터와 관렦하여 메모리 바라보기
차 례
• 포인터 변수
• 배열
• 문자열
• 포인터 : 메모리 주소
• 레퍼런스 변수
• 동적 할당
포인터
• 컴퓨터는 처리하는 모든 데이터를 주기억장
치에 저장한다.
• 포인터 : 주기억장치의 주소
• 포인터 변수 : 주소를 저장
• 일반 변수 : 값을 저장
300
…..
…...
0012FF60
값
주소
포인터 변수 1
• 변수
– 저장장소
– 변수는 사용하기 젂에 미리 선얶해야 한다.
– 변수는 사용하기 젂에 초기화 되어야 한다.
• 초기화하지 않은 경우 쓰레기 값이 저장되어 있다.
값을 저장하려면
일반 변수로 선얶
되어야 함!!
주소를 저장하려
면 포인터 변수로
선얶되어야 함!!
소스 4-2 (ch04_01_1.cpp)
#include <iostream>
using namespace std;
int main()
{
int sum=0, i=0;
for (i=1; i<=10; i++)
sum=sum+i;
cout << "1~10까지의 합 : " << sum << endl;
return 0;
}
중단점 찍고 단계별로 실행하면서 sum 변수의 주소, 변수 값 확인해보기
포인터 변수 2
• 변수 값 참조 – 일반변수 이름 사용
• 변수의 주소 확인(주소지정 연산자 & 사용)
소스 4-2 (ch04_01_1.cpp)
int a=100;;
cout << "a에 저장된 값 : " << a << endl;
cout << "a의 주소 : " << &a << endl;
return 0;
실행 결과
a에 저장된 값 : 100
a의 주소 : 0012FF60
포인터 변수 3
• 포인터 변수
– 변수의 주소를 저장
– 포인터 변수에 저장할 주소에 저장될 자료형과
포인터 변수의 자료형은 일치해야 함
(정수형 포인터 변수는 정수형 변수의 주소, 문자
형 포인터 변수는 문자형 변수의 주소를 저장함)
• 일반변수와 마찬가지로 사용 젂 선얶해야 함
자료형 일반변수이름;
자료형 *포인터변수이름;
일반변수와 포인터 변수
• 일반변수 : 값을 저장해야 할 경우 필요
• 포인터변수 : 변수의 주소를 저장해야 할 경
우 (동적 할당 또는 6장 함수에서 다뤄짐)
일반변수 포인터 변수 비고
선얶 자료형 변수이름; 자료형 *포변수이름;
& : 주소지정 연산자
* : 갂접 연산자
값 할당 변수이름=값;
포변수이름=주소;
*포변수이름=값;
선얶과 동시에
초기화
자료형 변수이름=값; 자료형 *포변수이름=주소;
„포인터변수이름‟을 „포변수이름‟으로 표기함
소스 4-4 (ch04_03.cpp)
#include <iostream>
using namespace std;
int main()
{
int a=100;;
int *pa;
pa=&a;
cout << "a에 저장된 값 : " << a << endl;
cout << "a의 주소 : " << &a << endl;
cout <<"******************************"<< endl;
cout << "a에 저장된 값 : " << *pa << endl;
cout << "a의 주소 : " << pa << endl;
return 0;
}
배열 1
• 변수
– 하나의 기억 공갂
– 예) 나이, 점수, 성별 등의 데이터를 저장
• 배열
– 연속적인 기억 공간
– 예) 30명의 총점을 저장, 세 과목의 성적을 저장
– 선얶할 때 연속적으로 필요한 기억 공갂의 개수
를 표시
배열 2
• 배열 선얶 : 자료형, 배열이름, 크기
• 배열 첨자는 0부터 시작
– int score[3]; 의 경우
score[0], score[1], score[2]
– char name[30];의 경우
name[0], name[1], …. , name[28], name[29]
자료형 배열이름[크기];
배열 3
• 배열이름 = 주소
int score[3];
&score[0] == score
&score[1] == score+1
&score[2] == score+2
char name[5];
&name[0] == name
&name[1] == name+1
&name[2] == name+2
&name[3] == name+3
&name[4] == name+4
정수형 배열의 경우 각 기억장소가
4바이트 단위로 증가함
문자형 배열의 경우 각 기억장소가
1바이트 단위로 증가함
소스 4-8 (ch04_05.cpp)
int score[3]={99,88,100};
cout << "score[0]의 주소 : " << &score[0] << endl;
cout << "score[1]의 주소 : " << &score[1] << endl;
cout << "score[2]의 주소 : " << &score[2] << endl;
cout << "******************************************" << endl;
for (int i=0; i<3; i++)
cout << "score[" << i << "]의 주소 : " << &score[i] << endl;
cout << "******************************************" << endl;
cout << "score[0]의 주소 : " << score << endl;
cout << "score[1]의 주소 : " << score+1 << endl;
cout << "score[2]의 주소 : " << score+2 << endl;
cout << "******************************************" << endl;
for (int i=0; i<3; i++)
cout << "score[" << i << "]의 주소 : " << score+i << endl;
배열 초기화
• 선얶과 동시에 초기화
자료형 배열이름[크기]={초기값1, 초기값2, ….};
자료형 배열이름[크기]={초기값, };
int s[3]={10,20, 30};
int a[5]={0,};
문자열
• 문자열 : 문자의 모음
• 문자 배열
– 문자열 상수는 쌍따옴표 “”로 표기
• 문자열 마지막을 알리는 널(NULL)문자가 자동으로 입
력됨
char string[30]=“computer”;
char string[30]={„c‟, „o‟, „m‟, „p‟, „u‟, „t‟, „e‟, „r‟, „0‟};
소스 4-11 (ch04_10.cpp)
#include <iostream>
using namespace std;
int main()
{
char string[30]="computer";
int i=0;
cout << string << endl;
for (i=0; string[i]!='0'; i++)
cout << string[i];
cout << endl;
return 0;
}
문자열 함수 1
• 자주 사용하는 문자열 처리 함수를 라이브러
리에서 제공함
• 문자열 길이 구하기
– strlen(const char *_Str)
• 문자열 복사하기
– strcpy(char *_Dest, const char *_Str)
strcpy_s(char *_Dest, rsize_t SizelnBytes, const
char *_Str)
문자열 함수 2
• 문자열 결합하기
– strcat(char *_Dest, const char *_Str)
– strcat_s(char *_Dest, rsize_t Sizeln Bytes, const
char *_Str)
• 문자열 비교하기
– strcmp(const char *str1, const char *str2)
소스 4-18 (ch04_14.cpp)
char s_string[100]="C++ programming is very interesting!!!";
char d_string[100];
cout << "s_string = " << s_string << endl;
strcpy_s(d_string, _countof(d_string), s_string);
//s_string를 d_string에 복사하기
cout << "d_string = " << d_string << endl;
strncpy_s(d_string, _countof(d_string), s_string, 3);
//s_string에서 3개의 문자를 d_string에 복사하기
cout << "d_string = " << d_string << endl;
strcat_s(d_string, _countof(d_string), "*****");
cout << "d_string = " << d_string << endl;
strncat_s(d_string, _countof(d_string), s_string, 3);
cout << "d_string = " << d_string << endl;
포인터 : 메모리 주소
• 포인터의 크기
– 주기억장치에서의 자료 처리 기본 단위
– 운영체제에 의해 결정, 윈도우 XP(32비트 운영체
제)
int *pi;
char *pc;
float *pf;
double *pd;
cout << "정수형 포인터 크기 : " << sizeof(pi) << endl;
cout << "문자형 포인터 크기 : " << sizeof(pc) << endl;
cout << "실수형 포인터 크기 : " << sizeof(pf) << endl;
cout << "배정도형 포인터 크기 : " << sizeof(pd) << endl;
레퍼런스 변수
• 이미 선얶한 변수를 다른 이름을 부르는 것
• 레퍼런스 변수 선얶시 반드시 초기화 해야 함!
자료형 &변수이름=변수;
소스 4-22 (ch04_18.cpp)
#include <iostream>
using namespace std;
int main()
{
int a=100;
int &ra=a;
cout << "a= " << a << endl;
cout << "ra=" << ra << endl;
cout << "****************" << endl;
ra=200;
cout << "a= " << a << endl;
cout << "ra=" << ra << endl;
return 0;
}
동적 할당 1
• 자료 저장을 위한 기억장소 할당 방법
– 정적 할당
• 프로그램에서 필요한 변수를 선얶
• 프로그램 실행 시작에서 필요한 변수에 대한 기억공갂
이 할당됨
– 동적 할당
• 프로그램에서 필요한 기억공갂의 크기를 할당하여 그
시작 주소를 기억 (포인터변수 사용!!)
• 프로그램 실행 중 기억공갂이 할당되고, 사용을 마친
후 할당한 기억공갂을 해제
동적 할당 2
• 기억장소 할당 : new
• 기억장소 해제 : delete
자료형 *포인터변수 = new 자료형[개수];
delete 포인터변수; //하나의 기억장소 해제
delete [ ] 포인터변수; //여러 개의 기억장소 해제
double *dp = new double; //한 개의 배정도형 기억공갂 할당
delete dp;
int *ip=new int[20];
delete [] ip;
소스 4-23 (ch04_19.cpp)
int *pi = new int;
*pi=100;
cout << *pi << endl;
delete pi;
int *pj = new int[3];
int i;
pj[0]=10; pj[1]=20; pj[2]=30;
for (i=0; i<3; i++)
cout << pj[i] << endl;
delete [] pj;
cout << *pi << endl;
Memory Layout

More Related Content

What's hot

R 프로그래밍 기본 문법
R 프로그래밍 기본 문법R 프로그래밍 기본 문법
R 프로그래밍 기본 문법
Terry Cho
 
10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스웅식 전
 
Ruby Enumerator(루비 열거자) 이해하기
Ruby Enumerator(루비 열거자) 이해하기Ruby Enumerator(루비 열거자) 이해하기
Ruby Enumerator(루비 열거자) 이해하기
Daegwon Kim
 
포인터와 배열
포인터와 배열포인터와 배열
포인터와 배열Kim YoSep
 
PostgreSql vaccum
PostgreSql vaccumPostgreSql vaccum
PostgreSql vaccum
승범 현
 
Binary Search
Binary SearchBinary Search
Binary Search
skku_npc
 
파이썬 데이터베이스 연결 1탄
파이썬 데이터베이스 연결 1탄파이썬 데이터베이스 연결 1탄
파이썬 데이터베이스 연결 1탄
SeongHyun Ahn
 
Data Structures
Data StructuresData Structures
Data Structures
skku_npc
 
Python Programming: Function
Python Programming: FunctionPython Programming: Function
Python Programming: Function
Chan Shik Lim
 
파이썬 데이터베이스 연결 2탄
파이썬 데이터베이스 연결 2탄파이썬 데이터베이스 연결 2탄
파이썬 데이터베이스 연결 2탄
SeongHyun Ahn
 
Java mentoring of samsung scsc 3
Java mentoring of samsung scsc   3Java mentoring of samsung scsc   3
Java mentoring of samsung scsc 3도현 김
 
Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706
Yong Joon Moon
 
ES6 for Node.js Study 3주차
ES6 for Node.js Study 3주차ES6 for Node.js Study 3주차
ES6 for Node.js Study 3주차
승빈이네 공작소
 
파이썬 웹프로그래밍 1탄
파이썬 웹프로그래밍 1탄 파이썬 웹프로그래밍 1탄
파이썬 웹프로그래밍 1탄
SeongHyun Ahn
 
이산치수학 Project7
이산치수학 Project7이산치수학 Project7
이산치수학 Project7KoChungWook
 
소켓 주소 구조체 다루기(윈도우 네트워크 프로그래밍)
소켓 주소 구조체 다루기(윈도우 네트워크 프로그래밍)소켓 주소 구조체 다루기(윈도우 네트워크 프로그래밍)
소켓 주소 구조체 다루기(윈도우 네트워크 프로그래밍)문익 장
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
Bill Kim
 
Python datatype
Python datatypePython datatype
Python datatype
건희 김
 

What's hot (20)

R 프로그래밍 기본 문법
R 프로그래밍 기본 문법R 프로그래밍 기본 문법
R 프로그래밍 기본 문법
 
10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스
 
Ruby Enumerator(루비 열거자) 이해하기
Ruby Enumerator(루비 열거자) 이해하기Ruby Enumerator(루비 열거자) 이해하기
Ruby Enumerator(루비 열거자) 이해하기
 
포인터와 배열
포인터와 배열포인터와 배열
포인터와 배열
 
Cp2 w5
Cp2 w5Cp2 w5
Cp2 w5
 
PostgreSql vaccum
PostgreSql vaccumPostgreSql vaccum
PostgreSql vaccum
 
Binary Search
Binary SearchBinary Search
Binary Search
 
파이썬 데이터베이스 연결 1탄
파이썬 데이터베이스 연결 1탄파이썬 데이터베이스 연결 1탄
파이썬 데이터베이스 연결 1탄
 
Data Structures
Data StructuresData Structures
Data Structures
 
Python Programming: Function
Python Programming: FunctionPython Programming: Function
Python Programming: Function
 
파이썬 데이터베이스 연결 2탄
파이썬 데이터베이스 연결 2탄파이썬 데이터베이스 연결 2탄
파이썬 데이터베이스 연결 2탄
 
Java mentoring of samsung scsc 3
Java mentoring of samsung scsc   3Java mentoring of samsung scsc   3
Java mentoring of samsung scsc 3
 
Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706
 
ES6 for Node.js Study 3주차
ES6 for Node.js Study 3주차ES6 for Node.js Study 3주차
ES6 for Node.js Study 3주차
 
파이썬 웹프로그래밍 1탄
파이썬 웹프로그래밍 1탄 파이썬 웹프로그래밍 1탄
파이썬 웹프로그래밍 1탄
 
이산치수학 Project7
이산치수학 Project7이산치수학 Project7
이산치수학 Project7
 
소켓 주소 구조체 다루기(윈도우 네트워크 프로그래밍)
소켓 주소 구조체 다루기(윈도우 네트워크 프로그래밍)소켓 주소 구조체 다루기(윈도우 네트워크 프로그래밍)
소켓 주소 구조체 다루기(윈도우 네트워크 프로그래밍)
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
 
Python datatype
Python datatypePython datatype
Python datatype
 

Viewers also liked

5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
12 1. const pointer, typedef
12 1. const pointer, typedef12 1. const pointer, typedef
12 1. const pointer, typedef웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 

Viewers also liked (9)

5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
Goorm class
Goorm classGoorm class
Goorm class
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
6. functions
6. functions6. functions
6. functions
 
6 function
6 function6 function
6 function
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
12 1. const pointer, typedef
12 1. const pointer, typedef12 1. const pointer, typedef
12 1. const pointer, typedef
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 

Similar to skku cp2 w4

02장 자료형과 연산자
02장 자료형과 연산자02장 자료형과 연산자
02장 자료형과 연산자웅식 전
 
HI-ARC PS 101
HI-ARC PS 101HI-ARC PS 101
HI-ARC PS 101
Jae-yeol Lee
 
05장 논리적 자료표현: 구조체
05장 논리적 자료표현: 구조체05장 논리적 자료표현: 구조체
05장 논리적 자료표현: 구조체
유석 남
 
Geveloper 160816
Geveloper 160816Geveloper 160816
Geveloper 160816
Nyeong Ahn
 
RNC C++ lecture_2 operator, if
RNC C++ lecture_2 operator, ifRNC C++ lecture_2 operator, if
RNC C++ lecture_2 operator, if
itlockit
 
10장 문자열 클래스와 파일 클래스
10장 문자열 클래스와 파일 클래스10장 문자열 클래스와 파일 클래스
10장 문자열 클래스와 파일 클래스
유석 남
 
게임프로그래밍입문 5주차
게임프로그래밍입문 5주차게임프로그래밍입문 5주차
게임프로그래밍입문 5주차
Yeonah Ki
 
[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기
Sang Heon Lee
 
03장 조건문, 반복문, 네임스페이스
03장 조건문, 반복문, 네임스페이스03장 조건문, 반복문, 네임스페이스
03장 조건문, 반복문, 네임스페이스
유석 남
 
14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿
유석 남
 
Cpp 0x kimRyungee
Cpp 0x kimRyungeeCpp 0x kimRyungee
Cpp 0x kimRyungeescor7910
 
Java mentoring of samsung scsc 0
Java mentoring of samsung scsc   0Java mentoring of samsung scsc   0
Java mentoring of samsung scsc 0
도현 김
 
5장 객체와클래스
5장 객체와클래스5장 객체와클래스
5장 객체와클래스
SeoYeong
 
RNC C++ lecture_4 While, For
RNC C++ lecture_4 While, ForRNC C++ lecture_4 While, For
RNC C++ lecture_4 While, For
itlockit
 
Python programming for Bioinformatics
Python programming for BioinformaticsPython programming for Bioinformatics
Python programming for Bioinformatics
Hyungyong Kim
 
7 mid term summary
7 mid term summary7 mid term summary
7 mid term summary웅식 전
 
이산수학 C1 프로젝트 7
이산수학 C1 프로젝트 7이산수학 C1 프로젝트 7
이산수학 C1 프로젝트 7pkok15
 
RNC C++ lecture_5 Array
RNC C++ lecture_5 ArrayRNC C++ lecture_5 Array
RNC C++ lecture_5 Array
itlockit
 

Similar to skku cp2 w4 (20)

06장 함수
06장 함수06장 함수
06장 함수
 
02장 자료형과 연산자
02장 자료형과 연산자02장 자료형과 연산자
02장 자료형과 연산자
 
HI-ARC PS 101
HI-ARC PS 101HI-ARC PS 101
HI-ARC PS 101
 
C review
C  reviewC  review
C review
 
05장 논리적 자료표현: 구조체
05장 논리적 자료표현: 구조체05장 논리적 자료표현: 구조체
05장 논리적 자료표현: 구조체
 
Geveloper 160816
Geveloper 160816Geveloper 160816
Geveloper 160816
 
RNC C++ lecture_2 operator, if
RNC C++ lecture_2 operator, ifRNC C++ lecture_2 operator, if
RNC C++ lecture_2 operator, if
 
10장 문자열 클래스와 파일 클래스
10장 문자열 클래스와 파일 클래스10장 문자열 클래스와 파일 클래스
10장 문자열 클래스와 파일 클래스
 
게임프로그래밍입문 5주차
게임프로그래밍입문 5주차게임프로그래밍입문 5주차
게임프로그래밍입문 5주차
 
[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기
 
03장 조건문, 반복문, 네임스페이스
03장 조건문, 반복문, 네임스페이스03장 조건문, 반복문, 네임스페이스
03장 조건문, 반복문, 네임스페이스
 
14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿
 
Cpp 0x kimRyungee
Cpp 0x kimRyungeeCpp 0x kimRyungee
Cpp 0x kimRyungee
 
Java mentoring of samsung scsc 0
Java mentoring of samsung scsc   0Java mentoring of samsung scsc   0
Java mentoring of samsung scsc 0
 
5장 객체와클래스
5장 객체와클래스5장 객체와클래스
5장 객체와클래스
 
RNC C++ lecture_4 While, For
RNC C++ lecture_4 While, ForRNC C++ lecture_4 While, For
RNC C++ lecture_4 While, For
 
Python programming for Bioinformatics
Python programming for BioinformaticsPython programming for Bioinformatics
Python programming for Bioinformatics
 
7 mid term summary
7 mid term summary7 mid term summary
7 mid term summary
 
이산수학 C1 프로젝트 7
이산수학 C1 프로젝트 7이산수학 C1 프로젝트 7
이산수학 C1 프로젝트 7
 
RNC C++ lecture_5 Array
RNC C++ lecture_5 ArrayRNC C++ lecture_5 Array
RNC C++ lecture_5 Array
 

More from 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)웅식 전
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide웅식 전
 

More from 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
 
W14 chap13
W14 chap13W14 chap13
W14 chap13
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
 

skku cp2 w4

  • 2. 차 례 • 포인터 변수 • 배열 • 문자열 • 포인터 : 메모리 주소 • 레퍼런스 변수 • 동적 할당
  • 3. 포인터 • 컴퓨터는 처리하는 모든 데이터를 주기억장 치에 저장한다. • 포인터 : 주기억장치의 주소 • 포인터 변수 : 주소를 저장 • 일반 변수 : 값을 저장 300 ….. …... 0012FF60 값 주소
  • 4. 포인터 변수 1 • 변수 – 저장장소 – 변수는 사용하기 젂에 미리 선얶해야 한다. – 변수는 사용하기 젂에 초기화 되어야 한다. • 초기화하지 않은 경우 쓰레기 값이 저장되어 있다. 값을 저장하려면 일반 변수로 선얶 되어야 함!! 주소를 저장하려 면 포인터 변수로 선얶되어야 함!!
  • 5. 소스 4-2 (ch04_01_1.cpp) #include <iostream> using namespace std; int main() { int sum=0, i=0; for (i=1; i<=10; i++) sum=sum+i; cout << "1~10까지의 합 : " << sum << endl; return 0; } 중단점 찍고 단계별로 실행하면서 sum 변수의 주소, 변수 값 확인해보기
  • 6. 포인터 변수 2 • 변수 값 참조 – 일반변수 이름 사용 • 변수의 주소 확인(주소지정 연산자 & 사용) 소스 4-2 (ch04_01_1.cpp) int a=100;; cout << "a에 저장된 값 : " << a << endl; cout << "a의 주소 : " << &a << endl; return 0; 실행 결과 a에 저장된 값 : 100 a의 주소 : 0012FF60
  • 7. 포인터 변수 3 • 포인터 변수 – 변수의 주소를 저장 – 포인터 변수에 저장할 주소에 저장될 자료형과 포인터 변수의 자료형은 일치해야 함 (정수형 포인터 변수는 정수형 변수의 주소, 문자 형 포인터 변수는 문자형 변수의 주소를 저장함) • 일반변수와 마찬가지로 사용 젂 선얶해야 함 자료형 일반변수이름; 자료형 *포인터변수이름;
  • 8. 일반변수와 포인터 변수 • 일반변수 : 값을 저장해야 할 경우 필요 • 포인터변수 : 변수의 주소를 저장해야 할 경 우 (동적 할당 또는 6장 함수에서 다뤄짐) 일반변수 포인터 변수 비고 선얶 자료형 변수이름; 자료형 *포변수이름; & : 주소지정 연산자 * : 갂접 연산자 값 할당 변수이름=값; 포변수이름=주소; *포변수이름=값; 선얶과 동시에 초기화 자료형 변수이름=값; 자료형 *포변수이름=주소; „포인터변수이름‟을 „포변수이름‟으로 표기함
  • 9. 소스 4-4 (ch04_03.cpp) #include <iostream> using namespace std; int main() { int a=100;; int *pa; pa=&a; cout << "a에 저장된 값 : " << a << endl; cout << "a의 주소 : " << &a << endl; cout <<"******************************"<< endl; cout << "a에 저장된 값 : " << *pa << endl; cout << "a의 주소 : " << pa << endl; return 0; }
  • 10. 배열 1 • 변수 – 하나의 기억 공갂 – 예) 나이, 점수, 성별 등의 데이터를 저장 • 배열 – 연속적인 기억 공간 – 예) 30명의 총점을 저장, 세 과목의 성적을 저장 – 선얶할 때 연속적으로 필요한 기억 공갂의 개수 를 표시
  • 11. 배열 2 • 배열 선얶 : 자료형, 배열이름, 크기 • 배열 첨자는 0부터 시작 – int score[3]; 의 경우 score[0], score[1], score[2] – char name[30];의 경우 name[0], name[1], …. , name[28], name[29] 자료형 배열이름[크기];
  • 12. 배열 3 • 배열이름 = 주소 int score[3]; &score[0] == score &score[1] == score+1 &score[2] == score+2 char name[5]; &name[0] == name &name[1] == name+1 &name[2] == name+2 &name[3] == name+3 &name[4] == name+4 정수형 배열의 경우 각 기억장소가 4바이트 단위로 증가함 문자형 배열의 경우 각 기억장소가 1바이트 단위로 증가함
  • 13. 소스 4-8 (ch04_05.cpp) int score[3]={99,88,100}; cout << "score[0]의 주소 : " << &score[0] << endl; cout << "score[1]의 주소 : " << &score[1] << endl; cout << "score[2]의 주소 : " << &score[2] << endl; cout << "******************************************" << endl; for (int i=0; i<3; i++) cout << "score[" << i << "]의 주소 : " << &score[i] << endl; cout << "******************************************" << endl; cout << "score[0]의 주소 : " << score << endl; cout << "score[1]의 주소 : " << score+1 << endl; cout << "score[2]의 주소 : " << score+2 << endl; cout << "******************************************" << endl; for (int i=0; i<3; i++) cout << "score[" << i << "]의 주소 : " << score+i << endl;
  • 14. 배열 초기화 • 선얶과 동시에 초기화 자료형 배열이름[크기]={초기값1, 초기값2, ….}; 자료형 배열이름[크기]={초기값, }; int s[3]={10,20, 30}; int a[5]={0,};
  • 15. 문자열 • 문자열 : 문자의 모음 • 문자 배열 – 문자열 상수는 쌍따옴표 “”로 표기 • 문자열 마지막을 알리는 널(NULL)문자가 자동으로 입 력됨 char string[30]=“computer”; char string[30]={„c‟, „o‟, „m‟, „p‟, „u‟, „t‟, „e‟, „r‟, „0‟};
  • 16. 소스 4-11 (ch04_10.cpp) #include <iostream> using namespace std; int main() { char string[30]="computer"; int i=0; cout << string << endl; for (i=0; string[i]!='0'; i++) cout << string[i]; cout << endl; return 0; }
  • 17. 문자열 함수 1 • 자주 사용하는 문자열 처리 함수를 라이브러 리에서 제공함 • 문자열 길이 구하기 – strlen(const char *_Str) • 문자열 복사하기 – strcpy(char *_Dest, const char *_Str) strcpy_s(char *_Dest, rsize_t SizelnBytes, const char *_Str)
  • 18. 문자열 함수 2 • 문자열 결합하기 – strcat(char *_Dest, const char *_Str) – strcat_s(char *_Dest, rsize_t Sizeln Bytes, const char *_Str) • 문자열 비교하기 – strcmp(const char *str1, const char *str2)
  • 19. 소스 4-18 (ch04_14.cpp) char s_string[100]="C++ programming is very interesting!!!"; char d_string[100]; cout << "s_string = " << s_string << endl; strcpy_s(d_string, _countof(d_string), s_string); //s_string를 d_string에 복사하기 cout << "d_string = " << d_string << endl; strncpy_s(d_string, _countof(d_string), s_string, 3); //s_string에서 3개의 문자를 d_string에 복사하기 cout << "d_string = " << d_string << endl; strcat_s(d_string, _countof(d_string), "*****"); cout << "d_string = " << d_string << endl; strncat_s(d_string, _countof(d_string), s_string, 3); cout << "d_string = " << d_string << endl;
  • 20. 포인터 : 메모리 주소 • 포인터의 크기 – 주기억장치에서의 자료 처리 기본 단위 – 운영체제에 의해 결정, 윈도우 XP(32비트 운영체 제) int *pi; char *pc; float *pf; double *pd; cout << "정수형 포인터 크기 : " << sizeof(pi) << endl; cout << "문자형 포인터 크기 : " << sizeof(pc) << endl; cout << "실수형 포인터 크기 : " << sizeof(pf) << endl; cout << "배정도형 포인터 크기 : " << sizeof(pd) << endl;
  • 21. 레퍼런스 변수 • 이미 선얶한 변수를 다른 이름을 부르는 것 • 레퍼런스 변수 선얶시 반드시 초기화 해야 함! 자료형 &변수이름=변수;
  • 22. 소스 4-22 (ch04_18.cpp) #include <iostream> using namespace std; int main() { int a=100; int &ra=a; cout << "a= " << a << endl; cout << "ra=" << ra << endl; cout << "****************" << endl; ra=200; cout << "a= " << a << endl; cout << "ra=" << ra << endl; return 0; }
  • 23. 동적 할당 1 • 자료 저장을 위한 기억장소 할당 방법 – 정적 할당 • 프로그램에서 필요한 변수를 선얶 • 프로그램 실행 시작에서 필요한 변수에 대한 기억공갂 이 할당됨 – 동적 할당 • 프로그램에서 필요한 기억공갂의 크기를 할당하여 그 시작 주소를 기억 (포인터변수 사용!!) • 프로그램 실행 중 기억공갂이 할당되고, 사용을 마친 후 할당한 기억공갂을 해제
  • 24. 동적 할당 2 • 기억장소 할당 : new • 기억장소 해제 : delete 자료형 *포인터변수 = new 자료형[개수]; delete 포인터변수; //하나의 기억장소 해제 delete [ ] 포인터변수; //여러 개의 기억장소 해제 double *dp = new double; //한 개의 배정도형 기억공갂 할당 delete dp; int *ip=new int[20]; delete [] ip;
  • 25. 소스 4-23 (ch04_19.cpp) int *pi = new int; *pi=100; cout << *pi << endl; delete pi; int *pj = new int[3]; int i; pj[0]=10; pj[1]=20; pj[2]=30; for (i=0; i<3; i++) cout << pj[i] << endl; delete [] pj; cout << *pi << endl;