Summary of Pointers and Arrays adopted from KNK






![배열: 포인터와의 관계 int foo[]= {380, 800, 777}; 주소 변수명 변수의 시작 주소를 사용한 배열: 포인터와의 관계 int foo[]= {380, 800, 777}; 주소 변수명 변수의 시작 주소를 사용한](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-7.jpg)

![포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; Address 0 포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; Address 0](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-9.jpg)
![포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; int *p, 포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; int *p,](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-10.jpg)
![포인터의 덧셈과 뺄셈 int p = q = foo[]= {10, 20, 30, 40, 50, 포인터의 덧셈과 뺄셈 int p = q = foo[]= {10, 20, 30, 40, 50,](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-11.jpg)
![포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; int *p, 포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; int *p,](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-12.jpg)
![포인터의 덧셈과 뺄셈 int p = q = foo[]= {10, 20, 30, 40, 50, 포인터의 덧셈과 뺄셈 int p = q = foo[]= {10, 20, 30, 40, 50,](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-13.jpg)
![Example of Array Processing with Pointers #define N 10 … int a[N], sum, *p; Example of Array Processing with Pointers #define N 10 … int a[N], sum, *p;](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-14.jpg)







- Slides: 21

Summary of Pointers and Arrays adopted from KNK C Programming : A Modern Approach



배열 되짚어 보기 • 배열: 같은 형의 값을 여러 개 저장할 수 있는 것과 그것의 시작 주소 int foo[]= {380, 800, 777}; 주소 변수명 0 x 00 c 777 foo[2] 0 x 008 800 foo[1] 0 x 004 380 foo[0] foo 0 x 000 Copyright © 2008 W. W. Norton & Company. All rights reserved. 4


![배열 포인터와의 관계 int foo 380 800 777 주소 변수명 변수의 시작 주소를 사용한 배열: 포인터와의 관계 int foo[]= {380, 800, 777}; 주소 변수명 변수의 시작 주소를 사용한](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-7.jpg)
배열: 포인터와의 관계 int foo[]= {380, 800, 777}; 주소 변수명 변수의 시작 주소를 사용한 변환 0 x 00 c 777 foo[2] foo + 2 x 4 byte 0 x 008 800 foo[1] foo + 1 x 4 byte 0 x 004 380 foo[0] 0 x 000 foo + 0 x 4 byte 기준 위치로 부터 X만큼 떨어진 위치를 접근할 수 있음 Copyright © 2008 W. W. Norton & Company. All rights reserved. 7

![포인터의 덧셈과 뺄셈 int foo 10 20 30 40 50 70 80 Address 0 포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; Address 0](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-9.jpg)
포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; Address 0 x 00 0 x 04 0 x 08 0 x 0 c 0 x 10 0 x 14 0 x 18 foo 10 20 30 40 50 60 70 80 foo[0] foo[1] foo[2] foo[3] foo[4] foo[5] foo[6] foo[7] index Copyright © 2008 W. W. Norton & Company. All rights reserved. 9
![포인터의 덧셈과 뺄셈 int foo 10 20 30 40 50 70 80 int p 포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; int *p,](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-10.jpg)
포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; int *p, *q; 0 xc 8 0 xe 8 p q Address 0 x 00 0 x 04 0 x 08 0 x 0 c 0 x 10 0 x 14 0 x 18 foo 10 20 30 40 50 60 70 80 foo[0] foo[1] foo[2] foo[3] foo[4] foo[5] foo[6] foo[7] index Copyright © 2008 W. W. Norton & Company. All rights reserved. 10
![포인터의 덧셈과 뺄셈 int p q foo 10 20 30 40 50 포인터의 덧셈과 뺄셈 int p = q = foo[]= {10, 20, 30, 40, 50,](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-11.jpg)
포인터의 덧셈과 뺄셈 int p = q = foo[]= {10, 20, 30, 40, 50, 70, 80}; *p, *q; &foo[3]; p + 2; 0 xc 8 0 xe 8 0 x 0 c 0 x 10 p q Address 0 x 00 0 x 04 0 x 08 0 x 0 c 0 x 10 0 x 14 0 x 18 foo 10 20 30 40 50 60 70 80 foo[0] foo[1] foo[2] foo[3] foo[4] foo[5] foo[6] foo[7] index Copyright © 2008 W. W. Norton & Company. All rights reserved. 11
![포인터의 덧셈과 뺄셈 int foo 10 20 30 40 50 70 80 int p 포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; int *p,](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-12.jpg)
포인터의 덧셈과 뺄셈 int foo[]= {10, 20, 30, 40, 50, 70, 80}; int *p, *q; p = &foo[3]; q = p + 2; 0 xc 8 p -= 2; 0 xe 8 0 x 04 0 x 10 p q Address 0 x 00 0 x 04 0 x 08 0 x 0 c 0 x 10 0 x 14 0 x 18 foo 10 20 30 40 50 60 70 80 foo[0] foo[1] foo[2] foo[3] foo[4] foo[5] foo[6] foo[7] index Copyright © 2008 W. W. Norton & Company. All rights reserved. 12
![포인터의 덧셈과 뺄셈 int p q foo 10 20 30 40 50 포인터의 덧셈과 뺄셈 int p = q = foo[]= {10, 20, 30, 40, 50,](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-13.jpg)
포인터의 덧셈과 뺄셈 int p = q = foo[]= {10, 20, 30, 40, 50, 70, 80}; *p, *q; &foo[5]; &foo[2]; printf(“%d”, p – q); 0 xc 8 0 xe 8 0 x 10 0 x 08 p q Address 0 x 00 0 x 04 0 x 08 0 x 0 c 0 x 10 0 x 14 0 x 18 foo 10 20 30 40 50 60 70 80 foo[0] foo[1] foo[2] foo[3] foo[4] foo[5] foo[6] foo[7] index Copyright © 2008 W. W. Norton & Company. All rights reserved. 13
![Example of Array Processing with Pointers define N 10 int aN sum p Example of Array Processing with Pointers #define N 10 … int a[N], sum, *p;](https://slidetodoc.com/presentation_image_h2/03597758c5addafc51d8125a783e664a/image-14.jpg)
Example of Array Processing with Pointers #define N 10 … int a[N], sum, *p; … sum = 0; for (p = &a[0]; p < &a[N]; p++) sum += *p; Copyright © 2008 W. W. Norton & Company. All rights reserved. 14


연산자의 결합 (exercise) Expression Meaning *p++ or *(p++) (*p)++ *++p or *(++p) ++*p or ++(*p) Copyright © 2008 W. W. Norton & Company. All rights reserved. 16

배열을 함수의 인자로 전달 Copyright © 2008 W. W. Norton & Company. All rights reserved. 17




함수 인자로 배열은 포인터처럼 동작 • 의미 4: • 배열의 일부만 전달 할 수 있음 int gon(int bar[], int n){ bar[1]++; } int main(void){ int foo = 10; int baz[10] = {0}; } gon(&baz[5], 3); Copyright © 2008 W. W. Norton & Company. All rights reserved. 21