SlideShare a Scribd company logo
1 of 19
Download to read offline
Pointer
2
Pointer Declaration and Assignment
§ Variables and address
– 메모리의 구조
– 각 변수는 메모리의 특정 주소에 위치 한다
• 변수의 주소: Pointer
…
1000 ‘a’
1001
3.2
1005
4
…
ch, 1byte
f, 4bytes
i, 4bytes
char ch = ‘a’ ;
float f = 3.2 ;
int i = 4 ;
address
3
Pointer Declaration and Assignment
§ Variables and address
– 변수부터 변수의 주소(Pointer) 얻어내기
char ch = ‘a’ ;
float f = 3.2 ;
int i = 4 ;
printf( “%d %d %dn”, &a, &f, &i ) ;
&(변수이름) == 변수의 주소 …
1000 ‘a’
1001
3.2
1005
4
…
Address
operator
4
Pointer Declaration and Assignment
§ Variables and address
– 변수의 주소(Pointer)를 이용하여 변수 접근하기
char ch ;
float f ;
int i ;
*(&ch) = ‘a’ ;
*(&f) = 3.2 ;
*(&i) = 4 ;
*(주소)는 (주소)에 위치한 변수를 뜻함
…
1000 ‘a’
1001
3.2
1005
4
…
char ch ;
float f ;
int i ;
ch = ‘a’ ;
f = 3.2 ;
i = 4 ;
Indirect
operator
5
Pointer Declaration and Assignment
§ Pointer Variable
– “변수의 주소(Pointer)”를 값을 저장할 수 있는 변수
char ch = ‘a’ ;
float f = 3.2 ;
int i = 4 ;
char *pch ;
float *pf ;
int *pi ;
pch = &ch ;
pf = &f ;
pi = &i ;
printf( “%d %d %dn”, pch, pf, pi ) ;
data_type * pointer_varaible;
char 변수의 주소만을
저장할 수 있는 변수
float 변수의 주소만을
저장할 수 있는 변수
int 변수의 주소만을
저장할 수 있는 변수
char *pch = &ch ;
float *pf = &f ;
int *pi = &i ;
6
Pointer Declaration and Assignment
§ Pointer Variable
char ch ;
float f ;
int i ;
ch = ‘a’ ;
f = 3.2 ;
i = 4 ;
char ch ;
float f ;
int i ;
char *pch = &ch ;
float *pf = &f ;
int *pi = &i ;
*pch = ‘a’ ;
*pf = 3.2 ;
*pi = 4 ;
…
1000 ‘a’
1001
3.2
1005
4
…
7
Pointer Declaration and Assignment
§ Pointer Variable
– 모든 pointer 변수는 4 bytes 크기이다 (4 bytes machine)
• why??
char *pch ;
float *pf ;
int *pi ;
printf( “%dn”, sizeof(pch) ) ;
printf( “%dn”, sizeof(pf) ) ;
printf( “%dn”, sizeof(pi) ) ;
int i = 0 ;
int *pi = & i;
printf( “%dn”, i ) ;
printf( “%dn”, *pi ) ;
printf( “%dn”, &pi ) ;
8
Pointer Declaration and Assignment
§ Pointer Variable
– 모든 pointer 변수도 변수이므로 주소를 갖는다.
int i = 0 ;
int *pi = &i;
printf( “%dn”, i ) ;
printf( “%dn”, *pi ) ;
printf( “%dn”, &pi ) ;
1000
…
1020
…
pi, 4 bytes
i, 4 bytes
9
Pointer Declaration and Assignment
[Ex] int *p;
int month=3;
p = &month;
pointer variable p에
month의 memory address를 assign
p month
3
p
1000
month
31000
주소를 직접 적는 것보다는
화살표를 이용하여 표시한다.
§ Example
10
Addressing and Dereferencing
[Ex] int a, b;
int *p;
a = 7;
b = 7;
p = &a;
printf(“*p = %dn”, *p);
*p = 3;
printf(“a = %dn”, a);
p는 a를 pointing하므로, *p == 7
p는 a의 address를 저장하고
있으므로, *p는 a를 의미하고,
결국 a의 내용을 3으로 변경
p에 a의 memory address를 assign
*p = 7
a = 3
§ Example
11
Addressing and Dereferencing
[Ex1] int a, b;
int *p;
a = b = 7;
p = &a;
*p = 3;
p = &b;
*p = 2 * *p – a;
pa
7
b
7
p
3
a b
7
pa
3 11
b
§ Example
12
Addressing and Dereferencing
[Ex1] int x, *p;
x = 10;
*p = x;
[Ex3] int x, *p;
x = 10;
p = x;
Error!!
p의 값이 주어지지 않아 p가 어느 곳을
refer하고 있는지를 모르는 상황에서
p가 point하는 곳에 x값 입력 불가
Error!!
p는 address를 값으로 하는 point
variable이므로 int의 assign은 불가
§ 하기 쉬운 에러
13
const 와 Pointer 변수
§ const 와 Pointer 변수
– p는 const int 형 변수의 주소를 저장할 수 있는 포인터 변수
– a는 상수변수로 값을 바꿀 수 없음.
– p가 가르키는 변수는 상수변수이므로 p를 통한 변경불가
const int a = 7;
const int *p = &a; // 가능
a = 8 ; //불가능
*p = 9 ; //불가능
14
const 와 Pointer 변수
§ const 와 Pointer 변수
– p는 const int 형 변수의 주소를 저장할 수 있는 포인터 변수이
지만, 일반 int 형 변수의 주소를 저장할 수 있음.
– a는 일반 변수로 값을 바꿀 수 있음.
– p가 가르키는 변수가 일반변수이기는 하나, p가 const int의 포
인터로 선언되었으므로, p를 통한 변경불가
int a = 7;
const int *p = &a; // 가능
a = 8 ; //가능
*p = 9 ; //불가능
15
const 와 Pointer 변수
§ const 와 Pointer 변수
– p는 일반 int 형 변수의 주소를 저장할 수 있는 포인터 변수이므
로, 상수변수 a의 주소를 저장할 수 없음.
– 만약 이것이 된다면 *p=8 이 가능하고 결국 a의 값을 바꿀 수
있게 됨.
const int a = 7;
int *p = &a; // 불가능
16
const 와 Pointer 변수
§ const 와 Pointer 변수
– p는 const int 형 변수의 주소를 저장할 수 있는 포인터 변수이
므로 모두 가능
– p는 const int 형 변수의 주소를 저장하는 상수 변수이므로, 첫
초기화는 가능하지만, 그 이후 치환은 불가능
const int a = 7, b = 8 ;
const int * p = &a; // 가능
p = &b; //가능
const int a = 7, b = 8 ;
const int * const p = &a; // 가능
p = &b; // 불가능
17
다중 Pointer 변수
§ 다중 Pointer 변수
– i는 int형 변수
– p는 int형 변수의 주소를 저장할 수 있는 포인터 변수
– q는 int형 변수의 주소를 저장하는 포인터 변수의 주소를 저장
할 수 있는 포인터 변수
• q도 포인터변수이기 때문에 크기는 4바이트이다(4 bytes machine)
int i = 4 ;
int *p ;
int **q ;
§ Example
18
Pointer Declaration and Assignment
[Ex] int i = 3;
int *p ;
int **q ;
p = &i ;
q = &p ;
p i
3
i
31000
p
10002000
q
20003000
q
§ Example:
– 각 단계의 메모리 상태를 그림으
로 그리시오.
– 최종적으로, i, j 변수의 값은 얼마
인가?
– i, j, p, q의 주소를 각각 1000,
1004, 2000, 3000 이라면, p, q, r
의 값은 각각 얼마인가?
19
Pointer Declaration and Assignment
[Ex] int i = 3, j = 2;
int *p, *q ;
int **r ;
p = &i ;
q = &j ;
r = &p ;
*p = 4 ;
*q = 5 ;
**r = 6 ;
*r = &q ;
q =&i ;
**r = 7 ;

More Related Content

What's hot

03. function in typescript
03. function in typescript03. function in typescript
03. function in typescriptHan JaeYeab
 
정규표현식의 이해와 활용
정규표현식의 이해와 활용정규표현식의 이해와 활용
정규표현식의 이해와 활용오석 한
 
자바 스터디(6기) 1
자바 스터디(6기) 1자바 스터디(6기) 1
자바 스터디(6기) 1Jina Lee
 
Hello python 3회 자료형과 함수(파이썬 스터디, 발표자료)
Hello python 3회 자료형과 함수(파이썬 스터디, 발표자료)Hello python 3회 자료형과 함수(파이썬 스터디, 발표자료)
Hello python 3회 자료형과 함수(파이썬 스터디, 발표자료)Cherucy
 
Lua 문법 -함수
Lua 문법 -함수Lua 문법 -함수
Lua 문법 -함수Jaehoon Lee
 
[Swift] Type inference
[Swift] Type inference[Swift] Type inference
[Swift] Type inferenceBill Kim
 
Ruby - 6th (루비 6장 변수와 식)
Ruby - 6th (루비 6장 변수와 식)Ruby - 6th (루비 6장 변수와 식)
Ruby - 6th (루비 6장 변수와 식)재영 이
 
변수 이름의 효과
변수 이름의 효과변수 이름의 효과
변수 이름의 효과민욱 이
 
[Effective Modern C++] Chapter1 - item2
[Effective Modern C++] Chapter1 - item2[Effective Modern C++] Chapter1 - item2
[Effective Modern C++] Chapter1 - item2지환 김
 
Basic study 2회차
Basic study 2회차Basic study 2회차
Basic study 2회차Seonmun Choi
 
[Effective Modern C++] Chapter1 - item1
[Effective Modern C++] Chapter1 - item1[Effective Modern C++] Chapter1 - item1
[Effective Modern C++] Chapter1 - item1지환 김
 
Secure coding-c-preprocessor-1
Secure coding-c-preprocessor-1Secure coding-c-preprocessor-1
Secure coding-c-preprocessor-1Seungyong Lee
 
C수업자료
C수업자료C수업자료
C수업자료koominsu
 
자료구조 그래프 보고서
자료구조 그래프 보고서자료구조 그래프 보고서
자료구조 그래프 보고서mil23
 

What's hot (20)

03. function in typescript
03. function in typescript03. function in typescript
03. function in typescript
 
07. type system
07. type system07. type system
07. type system
 
01. basic types
01. basic types01. basic types
01. basic types
 
정규표현식의 이해와 활용
정규표현식의 이해와 활용정규표현식의 이해와 활용
정규표현식의 이해와 활용
 
Lua 문법
Lua 문법Lua 문법
Lua 문법
 
자바 스터디(6기) 1
자바 스터디(6기) 1자바 스터디(6기) 1
자바 스터디(6기) 1
 
Hello python 3회 자료형과 함수(파이썬 스터디, 발표자료)
Hello python 3회 자료형과 함수(파이썬 스터디, 발표자료)Hello python 3회 자료형과 함수(파이썬 스터디, 발표자료)
Hello python 3회 자료형과 함수(파이썬 스터디, 발표자료)
 
Lua 문법 -함수
Lua 문법 -함수Lua 문법 -함수
Lua 문법 -함수
 
C++11
C++11C++11
C++11
 
[Swift] Type inference
[Swift] Type inference[Swift] Type inference
[Swift] Type inference
 
Ruby - 6th (루비 6장 변수와 식)
Ruby - 6th (루비 6장 변수와 식)Ruby - 6th (루비 6장 변수와 식)
Ruby - 6th (루비 6장 변수와 식)
 
변수 이름의 효과
변수 이름의 효과변수 이름의 효과
변수 이름의 효과
 
[Effective Modern C++] Chapter1 - item2
[Effective Modern C++] Chapter1 - item2[Effective Modern C++] Chapter1 - item2
[Effective Modern C++] Chapter1 - item2
 
Basic study 2회차
Basic study 2회차Basic study 2회차
Basic study 2회차
 
[Effective Modern C++] Chapter1 - item1
[Effective Modern C++] Chapter1 - item1[Effective Modern C++] Chapter1 - item1
[Effective Modern C++] Chapter1 - item1
 
Secure coding-c-preprocessor-1
Secure coding-c-preprocessor-1Secure coding-c-preprocessor-1
Secure coding-c-preprocessor-1
 
(닷넷, C#기초교육)C#선택적인수, 명명된 인수
(닷넷, C#기초교육)C#선택적인수, 명명된 인수(닷넷, C#기초교육)C#선택적인수, 명명된 인수
(닷넷, C#기초교육)C#선택적인수, 명명된 인수
 
7그룹 코드
7그룹 코드7그룹 코드
7그룹 코드
 
C수업자료
C수업자료C수업자료
C수업자료
 
자료구조 그래프 보고서
자료구조 그래프 보고서자료구조 그래프 보고서
자료구조 그래프 보고서
 

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웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
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(학생)웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료웅식 전
 

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
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
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
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
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(학생)
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 

9. pointer

  • 2. 2 Pointer Declaration and Assignment § Variables and address – 메모리의 구조 – 각 변수는 메모리의 특정 주소에 위치 한다 • 변수의 주소: Pointer … 1000 ‘a’ 1001 3.2 1005 4 … ch, 1byte f, 4bytes i, 4bytes char ch = ‘a’ ; float f = 3.2 ; int i = 4 ; address
  • 3. 3 Pointer Declaration and Assignment § Variables and address – 변수부터 변수의 주소(Pointer) 얻어내기 char ch = ‘a’ ; float f = 3.2 ; int i = 4 ; printf( “%d %d %dn”, &a, &f, &i ) ; &(변수이름) == 변수의 주소 … 1000 ‘a’ 1001 3.2 1005 4 … Address operator
  • 4. 4 Pointer Declaration and Assignment § Variables and address – 변수의 주소(Pointer)를 이용하여 변수 접근하기 char ch ; float f ; int i ; *(&ch) = ‘a’ ; *(&f) = 3.2 ; *(&i) = 4 ; *(주소)는 (주소)에 위치한 변수를 뜻함 … 1000 ‘a’ 1001 3.2 1005 4 … char ch ; float f ; int i ; ch = ‘a’ ; f = 3.2 ; i = 4 ; Indirect operator
  • 5. 5 Pointer Declaration and Assignment § Pointer Variable – “변수의 주소(Pointer)”를 값을 저장할 수 있는 변수 char ch = ‘a’ ; float f = 3.2 ; int i = 4 ; char *pch ; float *pf ; int *pi ; pch = &ch ; pf = &f ; pi = &i ; printf( “%d %d %dn”, pch, pf, pi ) ; data_type * pointer_varaible; char 변수의 주소만을 저장할 수 있는 변수 float 변수의 주소만을 저장할 수 있는 변수 int 변수의 주소만을 저장할 수 있는 변수 char *pch = &ch ; float *pf = &f ; int *pi = &i ;
  • 6. 6 Pointer Declaration and Assignment § Pointer Variable char ch ; float f ; int i ; ch = ‘a’ ; f = 3.2 ; i = 4 ; char ch ; float f ; int i ; char *pch = &ch ; float *pf = &f ; int *pi = &i ; *pch = ‘a’ ; *pf = 3.2 ; *pi = 4 ; … 1000 ‘a’ 1001 3.2 1005 4 …
  • 7. 7 Pointer Declaration and Assignment § Pointer Variable – 모든 pointer 변수는 4 bytes 크기이다 (4 bytes machine) • why?? char *pch ; float *pf ; int *pi ; printf( “%dn”, sizeof(pch) ) ; printf( “%dn”, sizeof(pf) ) ; printf( “%dn”, sizeof(pi) ) ; int i = 0 ; int *pi = & i; printf( “%dn”, i ) ; printf( “%dn”, *pi ) ; printf( “%dn”, &pi ) ;
  • 8. 8 Pointer Declaration and Assignment § Pointer Variable – 모든 pointer 변수도 변수이므로 주소를 갖는다. int i = 0 ; int *pi = &i; printf( “%dn”, i ) ; printf( “%dn”, *pi ) ; printf( “%dn”, &pi ) ; 1000 … 1020 … pi, 4 bytes i, 4 bytes
  • 9. 9 Pointer Declaration and Assignment [Ex] int *p; int month=3; p = &month; pointer variable p에 month의 memory address를 assign p month 3 p 1000 month 31000 주소를 직접 적는 것보다는 화살표를 이용하여 표시한다. § Example
  • 10. 10 Addressing and Dereferencing [Ex] int a, b; int *p; a = 7; b = 7; p = &a; printf(“*p = %dn”, *p); *p = 3; printf(“a = %dn”, a); p는 a를 pointing하므로, *p == 7 p는 a의 address를 저장하고 있으므로, *p는 a를 의미하고, 결국 a의 내용을 3으로 변경 p에 a의 memory address를 assign *p = 7 a = 3 § Example
  • 11. 11 Addressing and Dereferencing [Ex1] int a, b; int *p; a = b = 7; p = &a; *p = 3; p = &b; *p = 2 * *p – a; pa 7 b 7 p 3 a b 7 pa 3 11 b § Example
  • 12. 12 Addressing and Dereferencing [Ex1] int x, *p; x = 10; *p = x; [Ex3] int x, *p; x = 10; p = x; Error!! p의 값이 주어지지 않아 p가 어느 곳을 refer하고 있는지를 모르는 상황에서 p가 point하는 곳에 x값 입력 불가 Error!! p는 address를 값으로 하는 point variable이므로 int의 assign은 불가 § 하기 쉬운 에러
  • 13. 13 const 와 Pointer 변수 § const 와 Pointer 변수 – p는 const int 형 변수의 주소를 저장할 수 있는 포인터 변수 – a는 상수변수로 값을 바꿀 수 없음. – p가 가르키는 변수는 상수변수이므로 p를 통한 변경불가 const int a = 7; const int *p = &a; // 가능 a = 8 ; //불가능 *p = 9 ; //불가능
  • 14. 14 const 와 Pointer 변수 § const 와 Pointer 변수 – p는 const int 형 변수의 주소를 저장할 수 있는 포인터 변수이 지만, 일반 int 형 변수의 주소를 저장할 수 있음. – a는 일반 변수로 값을 바꿀 수 있음. – p가 가르키는 변수가 일반변수이기는 하나, p가 const int의 포 인터로 선언되었으므로, p를 통한 변경불가 int a = 7; const int *p = &a; // 가능 a = 8 ; //가능 *p = 9 ; //불가능
  • 15. 15 const 와 Pointer 변수 § const 와 Pointer 변수 – p는 일반 int 형 변수의 주소를 저장할 수 있는 포인터 변수이므 로, 상수변수 a의 주소를 저장할 수 없음. – 만약 이것이 된다면 *p=8 이 가능하고 결국 a의 값을 바꿀 수 있게 됨. const int a = 7; int *p = &a; // 불가능
  • 16. 16 const 와 Pointer 변수 § const 와 Pointer 변수 – p는 const int 형 변수의 주소를 저장할 수 있는 포인터 변수이 므로 모두 가능 – p는 const int 형 변수의 주소를 저장하는 상수 변수이므로, 첫 초기화는 가능하지만, 그 이후 치환은 불가능 const int a = 7, b = 8 ; const int * p = &a; // 가능 p = &b; //가능 const int a = 7, b = 8 ; const int * const p = &a; // 가능 p = &b; // 불가능
  • 17. 17 다중 Pointer 변수 § 다중 Pointer 변수 – i는 int형 변수 – p는 int형 변수의 주소를 저장할 수 있는 포인터 변수 – q는 int형 변수의 주소를 저장하는 포인터 변수의 주소를 저장 할 수 있는 포인터 변수 • q도 포인터변수이기 때문에 크기는 4바이트이다(4 bytes machine) int i = 4 ; int *p ; int **q ;
  • 18. § Example 18 Pointer Declaration and Assignment [Ex] int i = 3; int *p ; int **q ; p = &i ; q = &p ; p i 3 i 31000 p 10002000 q 20003000 q
  • 19. § Example: – 각 단계의 메모리 상태를 그림으 로 그리시오. – 최종적으로, i, j 변수의 값은 얼마 인가? – i, j, p, q의 주소를 각각 1000, 1004, 2000, 3000 이라면, p, q, r 의 값은 각각 얼마인가? 19 Pointer Declaration and Assignment [Ex] int i = 3, j = 2; int *p, *q ; int **r ; p = &i ; q = &j ; r = &p ; *p = 4 ; *q = 5 ; **r = 6 ; *r = &q ; q =&i ; **r = 7 ;