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
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 ;