Pro 19 1d pointer array

Page 1

2014-2 κΈ°μ΄ˆν”„λ‘œκ·Έλž˜λ° 2

19.1차원 포인터와 λ°°μ—΄


제1μž₯

1차원 포인터와 λ°°μ—΄ 1.1

포인터와 λ°°μ—΄μ˜ 차이점

λ‹€μŒ ν”„λ‘œκ·Έλž¨μ˜ 였λ₯˜λ₯Ό μ„€λͺ…ν•˜κ³  μˆ˜μ •ν•΄λ³΄μž. 1.1.0 λ¬Όλ¦¬μ μ£Όμ†ŒλŠ” μƒμˆ˜ 1 2

int main ( )

3

{

4

char carr[3] ={’A’, ’B’, ’C’};

5

char *cp = carr;

6

printf("%d %d\n", sizeof(cp), sizeof(carr) );

7 8

cp = NULL;

9

carr = NULL;

10 11

}

12

//carr = NULL; //μƒμˆ˜λ‘œ λŒ€μž… λΆˆκ°€. error C2106: ’=’ : left operand must be l-value

1.1.1

error C2106: ’=’ : left operand must be l-value

μƒμˆ˜μ— 값을 λ„£μœΌλ €κ³  ν•˜λŠ” 행동 1.1.1 μƒμˆ˜μ— 값을 λ„£μœΌλ €κ³  ν•˜λŠ” 행동 예 1

#include <stdio.h>

2

#define NUM 3

3

void main ( )

4

{

5

char* a[4]={"kim", "choi", "park", "jung"};

6

char b[4][5]={"kim", "choi", "park", "jung"};

7

NUM = 5; // error C2106: ’=’ : left operand must be l-value

8

a[2]=a[3];

9

b[2]=b[3]; //error C2106: ’=’ : left operand must be l-value ??

10 11

}

2


포인터와 λ°°μ—΄μ˜ 차이점

1.1.2

ν¬μΈν„°λŠ” λ³€μˆ˜? 배열은 μƒμˆ˜ ?

Figure 1.1: ν¬μΈν„°λŠ” λ³€μˆ˜

Figure 1.2: 배열은 μƒμˆ˜

3


제 1 μž₯ 1차원 포인터와 λ°°μ—΄

1.1.3

정리 ν•΄μ•Ό ν•˜λŠ” λͺ‡ 가지 Error

1.1.2 pAλŠ” μ“°λ ˆκΈ° κ°’μœΌλ‘œ μ΄ˆκΈ°ν™” 됨 1

int main ( )

2

{

3

int *pA;

4

*pA=10; return 0;

5 6

}

7

1.1.3 μ£Όμ†Œμž„μ˜μ§€μ •λΆˆκ°€ Compile Time Error !! 1

int main ( )

2

{

3

int *pA = 0x00123456;

4

*pA=10; return 0;

5 6

}

1.1.4 p[0] = ’L’; μƒμˆ˜ μ˜μ—­ μ ‘κ·Ό μ‹œλ„ Run-time Error!! 1

int main ( )

2

{

3

char a[4] = "KIM";

4

char *p = "KIM";

5

p[0] = ’L’;

6

a[0] = ’L’;

7

}

8

1.1.5 OK !! 1

int main ( )

2

{

3

char a[4] = "KIM";

4

char *p = a;

5

p[0] = ’L’; a[0] = ’L’;

6 7 8

4

}


λ°°μ—΄κ³Ό 포인터 μ΄λ¦„μ˜ ν™œμš© arr[i] = *(arr+i)

1.2

λ°°μ—΄κ³Ό 포인터 μ΄λ¦„μ˜ ν™œμš© arr[i] = *(arr+i)

λ°°μ—΄ 이름을 ν¬μΈν„°μ²˜λŸΌ, ν¬μΈν„°λ³€μˆ˜ 이름을 λ°°μ—΄ μ΄λ¦„μ²˜λŸΌ ν™œμš© ν•˜λŠ”κ²ƒμ΄ κ°€λŠ₯ 함

Figure 1.3: λ°°μ—΄κ³Ό 포인터 μ΄λ¦„μ˜ ν™œμš©

1.2.0 포인터와 λ°°μ—΄μ˜ 관계 1

int main ( )

2

{

3

int arr[3] = {0,1,2};

4

int *ptr = arr;

5

printf("%d %d %d \n", *(ptr+0),*(ptr+1),*(ptr+2));

6

printf("%d %d %d \n", ptr[0],ptr[1],ptr[2]);

7 8

}

9

---------- Run GCC ----------

10

0 1 2

11

0 1 2

5


제 1 μž₯ 1차원 포인터와 λ°°μ—΄

포인터 μ—°μ‚°

1.3 1.3.1

λ°°μ—΄κ³Ό 포인터 μ—°μ‚°μ˜ 의미 차이

β€’ ν¬μΈν„°λŠ” μ£Όμ†Œμ΄λ―€λ‘œ 기본적으둜 +- 연산이 κ°€λŠ₯ν•˜λ‹€. β€’ ν•˜μ§€λ§Œ μ£Όμ†Œλ₯Ό λ³€κ²½ν•˜κ³ μž ν• κ²½μš°λŠ” λ¬Όλ¦¬μ μ£Όμ†ŒμΈκ²½μš°(λ©”λͺ¨λ¦¬μ˜ μ£Όμ†Œ)λŠ” 변경이 λΆˆκ°€λŠ₯ν•˜κ³ , 포인터변 수의 값은 μ£Όμ†Œλ³€μˆ˜μ΄λ―€λ‘œ λ³€κ²½ κ°€λŠ₯ν•˜λ‹€. β€’ λŒ€ν‘œμ μΈ 예둜 λ°°μ—΄μ˜ 이름은 λ¬Όλ¦¬μ μ£Όμ†Œ μ΄λ―€λ‘œ λ³€κ²½ λΆˆκ°€λŠ₯ν•˜λ‹€.

1.3.0 λ°°μ—΄μ˜ 이름은 물리적 μ£Όμ†Œμ΄λ―€λ‘œ λ³€κ²½ λΆˆκ°€λŠ₯ 1

int main ( )

2

{ int arr[] = {1};

3

arr++; //error

4 5

}

1.3.1 μ¦κ°μ—°μ‚°μžμ˜ 잘λͺ»λœ μ‚¬μš© 1

int main ( )

2

{

3

int arr1[2];

4

char arr2[2];

5

double arr3[2];

6 7

printf("%d λ²ˆμ§€, %d λ²ˆμ§€, %dλ²ˆμ§€\n", arr1++,arr2++,arr3++); //error

8

printf("%d λ²ˆμ§€, %d λ²ˆμ§€, %dλ²ˆμ§€\n", arr1+1,arr2+1,arr3+1); //ok

9 10 11

6

}


포인터 μ—°μ‚°

1.3.2

포인터 연산을 ν†΅ν•œ λ°°μ—΄ μš”μ†Œμ˜ μ ‘κ·Ό

1.3.2 μ¦κ°μ—°μ‚°μžμ˜ μ΄μš©ν•œ λ°°μ—΄ κ°’μ˜ λ³€κ²½ 1

int main ( )

2

{

3

int arr[] = {1,2,3,4,5};

4

int* parr = arr;

5

printf("%d \n", *parr);

6

printf("%d \n", *(++parr));

7

printf("%d \n", *(++parr));

8

printf("%d \n", *(parr+1)); printf("%d \n", *(parr+2));

9 10

}

11

---------- Run GCC ----------

12

1

13

2

14

3

15

4

16

5

1.3.3 μ—°μ‚°μž μš°μ„ μˆœμœ„λ₯Ό μ΄μš©ν•œ λ°°μ—΄ κ°’μ˜ λ³€κ²½ 1

int main ( )

2

{

3

int arr[] = {10,20,30,40,50};

4

int* parr = arr;

5

printf("%d \n", *parr++);

6

printf("%d \n", (*parr)++);

7

printf("%d \n", *parr);

8

printf("%d \n", *parr+1);

9

printf("%d \n", *(parr+1)); printf("%d \n", *(parr+2));

10 11

}

12

---------- Run GCC ----------

13

10

14

20

15

21

16

22

17

30

18

40

7


제 1 μž₯ 1차원 포인터와 λ°°μ—΄

1.3.4 μ—°μ‚°μž μš°μ„ μˆœμœ„λ₯Ό μ΄μš©ν•œ λ°°μ—΄ κ°’μ˜ λ³€κ²½ 1

int main ( )

2

{

3

int arr[] = {10,20,30,40,50};

4

int* parr = arr;

5

printf("%d \n", *(parr=parr+2));

6

printf("%d \n", *(parr++));

7

printf("%d \n", parr[-2]);

8

printf("%d \n", *(parr+1)); printf("%d \n", *(++parr));

9

8

10

}

11

---------- Run GCC ----------

12

30

13

30

14

20

15

50

16

50


λ¬Έμžμ—΄ μƒμˆ˜λ₯Ό κ°€λ¦¬ν‚€λŠ” 포인터

λ¬Έμžμ—΄ μƒμˆ˜λ₯Ό κ°€λ¦¬ν‚€λŠ” 포인터

1.4 1.4.1

λ¬Έμžμ—΄ ν‘œν˜„ λ°©μ‹μ˜ 이해

1.4.0 λ°°μ—΄κΈ°λ°˜μ˜ λ¬Έμžμ—΄ λ³€μˆ˜ 1

void main(void)

2

{ char str[10]="Hello";

3 4

}

1.4.1 포인터 기반의 λ¬Έμžμ—΄ μƒμˆ˜ 1

void main(void)

2

{ char *str="Hello";

3 4

}

1.4.2 λ°°μ—΄ 포인터 기반의 λ¬Έμžμ—΄ λ³€μˆ˜ 1

void main(void)

2

{ char tmp[] = "Hello";

3

char *str=tmp;

4 5

}

1.4.3 λ¬Έμžμ—΄μ²˜λ¦¬ ν•¨μˆ˜(strcpy)λ₯Ό μ΄μš©ν•œ λ¬Έμžμ—΄ λ³€μˆ˜ 1

int main(void)

2

{

3

char tmp[10];

4

strcpy(tmp,"Hello"); return 0;

5 6

}

9


제 1 μž₯ 1차원 포인터와 λ°°μ—΄

1.4.4 Heap λ©”λͺ¨λ¦¬μ™€ λ¬Έμžμ—΄μ²˜λ¦¬ ν•¨μˆ˜λ₯Ό μ΄μš©ν•œ λ¬Έμžμ—΄ λ³€μˆ˜ 1

int main(void)

2

{

3

char* tmp;

4

tmp = (char*)malloc(10);

5

strcpy(tmp,"Hello");

6

return 0;

7

}

1.4.5 μ„œμ‹μ„ ν¬ν•¨ν•œ λ¬Έμžμ—΄ 생성 방법 sprintf() ν™œμš© 1

#include <stdio.h>

2

#include <stdlib.h>

3 4

void main(int argc, char* argv[])

5

{

6

char str[50];

7

sprintf(str,"Hello %s\n","sprintf()"); printf("%s",str);

8 9

10

}


λ°°μ—΄κ³Ό ν¬μΈν„°μ˜ μ‘μš© 두 가지

1.5 1.5.1

λ°°μ—΄κ³Ό ν¬μΈν„°μ˜ μ‘μš© 두 가지 포인터 λ°°μ—΄

β€’ λ°°μ—΄μ˜ μš”μ†Œλ‘œ 포인터λ₯Ό μ§€λ‹ˆλŠ” λ°°μ—΄ β€’ 배열에 μ‹€μ œ λ‹΄κΈ°λŠ” 값은? μ£Όμ†Œ

1.5.0 μ£Όμ†Œκ°’μ„ λ‹΄λŠ” λ°°μ—΄μ˜ λͺ¨μŠ΅ 1

#include <stdio.h>

2 3

int main(void)

4

{

5

int a=10, b=20, c=30;

6

int* arr[3]={&a, &b, &c};

7 8

printf("%d \n", *arr[0]);

9

printf("%d \n", *arr[1]);

10

printf("%d \n", *arr[2]);

11

return 0;

12 13

}

14

---------- Run GCC ----------

15

10

16

20

17

30

1.5.1 포인터 배열을 μ΄μš©ν•œ 볡수의 λ¬Έμžμ—΄ λ‹΄κΈ° 예 1

#include <stdio.h>

2 3

#include <stdio.h>

4 5

int main(void)

6

{

7

char* arr[3]={

8

"Hello World",

9

"C Programming",

10

"java Programming"

11

};

12

printf("%s \n", arr[0]);

13

printf("%s \n", arr[1]);

14

printf("%s \n", arr[2]);

15

return 0;

16 17

}

18

---------- Run GCC ----------

19

Hello World

20

C Programming

21

java Programming

11


제 1 μž₯ 1차원 포인터와 λ°°μ—΄

1.5.2

λ°°μ—΄ 포인터

β€’ 배열을 κ°€λ¦¬ν‚€λŠ” 포인터 β€’ int (* arr1)[10];

12


μ—°μŠ΅λ¬Έμ œ

μ—°μŠ΅λ¬Έμ œ

1.6

Question

1

μ•„λž˜ ν”„λ‘œκ·Έλž¨μ˜ λ™μž‘μ„ λ©”λͺ¨λ¦¬λ§΅μ„ κ·Έλ €μ„œ μ„€λͺ…ν•΄λ³΄μ‹œμ˜€. 두 번째 ν”„λ‘œκ·Έλž¨μ€ Run-time error κ°€ λ°œμƒν•œλ‹€ 원인을 μ„€λͺ…ν•˜μ‹œμ˜€. 1.6.0 문제

1

(1)

2

void main(void)

3

{

4

char str[100];

5

scanf("%s",str); printf("%s",str);

6 7

}

8

(2)

9

void main(void)

10

{

11

char *str;

12

scanf("%s",str); printf("%s",str);

13 14

}

μ •λ‹΅ 1.6.1 fgets μ •λ‹΅ 1

#include <stdio.h>

2

2

λ‹€μŒ ν”„λ‘œκ·Έλž¨μ˜ μ—λŸ¬ 원인을 λ©”λͺ¨λ¦¬λ§΅μ„ κ·Έλ €μ„œ μ„€λͺ…ν•˜μ‹œμ˜€ 1.6.2 문제

1

int main()

2

{

3

char str1[5]="abcd";

4

char *str2="ABCD";

5 6

printf("%s \n", str1);

7

printf("%s \n", str2);

8 9

str1[0]=’x’;

10

str2[0]=’x’;

11 12

printf("%s \n", str1);

13

printf("%s \n", str2);

13


제 1 μž₯ 1차원 포인터와 λ°°μ—΄ 14

return 0;

15 16

}

μ •λ‹΅ 1.6.3 fgets μ •λ‹΅ 1

#include <stdio.h>

2

3

μ•„λž˜ν”„λ‘œκ·Έλž¨μ—μ„œ β€œHello Worldβ€μ˜β€˜Wβ€™λ§Œ 좜λ ₯ν•΄λ³΄μ‹œμ˜€ 1.6.4 문제

1

int main(void)

2

{

3

char* arr[3]={

4

"Hello World",

5

"C Programming",

6

"java Programming"

7

};

8

printf("%c\n", ______________________); return 0;

9 10

}

μ •λ‹΅ 1.6.5 fgets μ •λ‹΅ 1

#include <stdio.h>

2

int main(void)

3

{

4

char* arr[3]={

5

"Hello World",

6

"C Programming",

7

"java Programming"

8

};

9

printf("%c\n", *(arr[0]+6)); printf("%c\n", arr[0][6]);

10

return 0;

11 12

4

}

λ‹€μŒ ν”„λ‘œκ·Έλž¨μ˜ λ©”λͺ¨λ¦¬λ§΅κ³Ό μ‹€ν–‰κ²°κ³Όλ₯Ό μž‘μ„±ν•˜μ‹œμ˜€. 1.6.6 문제

14


μ—°μŠ΅λ¬Έμ œ

1

int main(int argc, char* argv[])

2

{

3

int arr[5] = {9,8,7,6,5};

4

int *pt =arr;

5

int i;

6 7

printf("%d\n",*pt+3);

8

printf("%d\n",*(pt+=3));

9 10

printf("%d\n",*pt++);

11

printf("%d\n",*pt);

12 13

printf("%d %d %d\n",*pt<<2, *pt, (*pt)++);

14

printf("%d\n",*pt);

15

printf("%d %d\n",pt[-1],*(arr+2));

16 17

}

μ •λ‹΅ 1.6.7 fgets μ •λ‹΅ 1

int main(int argc, char* argv[])

2

{

3

int arr[5] = {9,8,7,6,5};

4

int *pt =arr;

5

int i;

6 7

printf("%d\n",*pt+3);

8

printf("%d\n",*(pt+=3));

9 10

printf("%d\n",*pt++);

11

printf("%d\n",*pt);

12 13

printf("%d %d %d\n",*pt<<2, *pt, (*pt)++);

14

printf("%d\n",*pt);

15

printf("%d %d\n",pt[-1],*(arr+2));

16

5

17

}

18

---------- Run GCC ----------

19

12

20

6

21

6

22

5

23

24 6 5

24

6

25

6 7

λ‹€μŒ ν”„λ‘œκ·Έλž¨μ˜ λ©”λͺ¨λ¦¬λ§΅κ³Ό μ‹€ν–‰κ²°κ³Όλ₯Ό μž‘μ„±ν•˜μ‹œμ˜€. 1.6.8 문제

15


제 1 μž₯ 1차원 포인터와 λ°°μ—΄ 1

int main(int argc, char* argv[])

2

{

3

char carr[] = "ABCDEFGHI";

4

char *pt = carr + 2;

5

char *pt2 = pt + 2;

6

printf("%x \n",*pt2+=3);

7 8

printf("%d \n",*(pt+=1));

9 10

printf("%x \n",*(--pt));

11 12

printf("%c \n",*pt++);

13 14

printf("%d %d %d \n",*pt<<1, *pt--, (*pt)++);

15 16

printf("%d \n",(*pt2 ^ *pt++));

17 18

printf("%c %d \n",pt[-1],*(carr-2));

19 20

}

μ •λ‹΅ 1.6.9 fgets μ •λ‹΅ 1

int main(int argc, char* argv[])

2

{

3

char carr[] = "ABCDEFGHI";

4

char *pt = carr + 2;

5

char *pt2 = pt + 2;

6

printf("%x \n",*pt2+=3);

7 8

printf("%d \n",*(pt+=1));

9 10

printf("%x \n",*(--pt));

11 12

printf("%c \n",*pt++);

13 14

printf("%d %d %d \n",*pt<<1, *pt--, (*pt)++);

15 16

printf("%d \n",(*pt2 ^ *pt++));

17 18

printf("%c %d \n",pt[-1],*(carr-2));

19 20

}

21

16

22

48

23

68

24

43

25

C

26

134 69 68

27

11

28

C 126


μ—°μŠ΅λ¬Έμ œ

6

λ‹€μŒ ν”„λ‘œκ·Έλž¨μ€ μˆ˜μ‹μ„ μž…λ ₯ λ°›μ•„ μ—°μ‚°ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ΄λ‹€. μ‚¬μš©μž μ •μ˜ν•¨μˆ˜ calculator() λ₯Ό μ™„μ„± ν•˜μ‹œμ˜€ 1.6.10 문제

1

float calculator(char*);

2 3

int main(int argc, char* argv[])

4

{

5

float result=0;

6

char str[1024];

7

printf("μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :");

8

scanf("%s",str);

9

while(1){

10

result = calculator(str);

11

printf("%s = %f\n", str,result);

12

printf("μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :");

13

scanf("%s",str); }

14

return 0;

15 16

}

17 18

μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :4+3

19

4+3 = 7.000000

20

μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :4-3

21

4-3 = 1.000000

22

μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :4*3

23

4*3 = 12.000000

24

μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :4/3

25

4/3 = 1.000000

26

μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :

μ •λ‹΅ 1.6.11 fgets μ •λ‹΅ 1

#include <stdio.h>

2 3

float calculator(char*);

4 5

int main(int argc, char* argv[])

6

{

7

float result=0;

8

char str[1024];

9

printf("μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :");

10

scanf("%s",str);

11

while(1){

12

result = calculator(str);

13

printf("%s = %f\n", str,result);

14

printf("μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :"); scanf("%s",str);

15 16

}

17

return 0;

17


제 1 μž₯ 1차원 포인터와 λ°°μ—΄ 18

}

19

float calculator(char* str)

20

{

21

float result=0;

22

int a=0,b=0;

23

int a_reg=1,b_reg=1;

24

char op;

25

while(*str != 0)

26

{

27

if(a_reg && *str >= ’0’ && *str <= ’9’)

28

{ a = *str - 48;

29

a_reg = 0;

30 31

}

32

else if(b_reg && *str >= ’0’ && *str <= ’9’)

33

{ b = *str - 48;

34

b_reg = 0;

35 36

}

37

else if(*str == ’+’ || *str == ’-’ || *str == ’*’ || *str == ’/’ )

38

{ op = *str;

39

}

40

str++;

41 42

}

43

switch(op)

44

{ case ’+’ :

45

result = a + b;

46

break;

47

case ’-’:

48

result = a - b;

49

break;

50

case ’*’:

51

result = a * b;

52

break;

53

case ’/’:

54

result = a / b;

55

break;

56 57

}

58

return result;

59

}

1.6.12 λ‹€λ₯Έλ°©λ²• 1

#include <stdio.h>

2

#include <stdlib.h>

3

#include <math.h>

4

#include <string.h>

5

18

6

float calculator(char* str)

7

{

8

int op_pos = 0;

9

int end_pos = strlen(str);


μ—°μŠ΅λ¬Έμ œ

10

char op1[10],op2[10];

11

while(1)

12

{ if(str[op_pos] == ’+’ || str[op_pos] == ’-’ || str[op_pos] == ’*’ || str[op_pos] == ’/’)

13

break;

14

else

15

op_pos++;

16 17

}

18

int i;

19

for(i=0; i<op_pos; i++) op1[i] = str[i];

20 21

op1[i] = ’\0’;

22

i++;

// μ—°μ‚°μž 자리수 λ¬΄μ‹œ

23 24

int j;

25

for(j=i; j<end_pos; j++)

26

op2[j-i] = str[j];

27

op2[j-i] = ’\0’;

28 29

float result=0;

30

switch(str[op_pos])

31

{

32

case ’+’ :

33

result = atoi(op1) + atoi(op2);

34

break;

35

case ’-’:

36

result = atoi(op1) - atoi(op2);

37

break;

38

case ’*’:

39

result = atoi(op1) * atoi(op2);

40

break;

41

case ’/’:

42

result = atoi(op1) / atoi(op2); break;

43

}

44

return result;

45 46

}

47 48 49

int main(int argc, char* argv[])

50

{

51

float result=0;

52

char str[1024];

53

printf("μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :");

54

scanf("%s",str);

55

while(1){

56

result = calculator(str);

57

printf("%s = %f\n", str,result);

58

printf("μˆ˜μ‹μ„ μž…λ ₯ν•˜μ‹œμ˜€. :"); scanf("%s",str);

59

}

60

return 0;

61 62

}

19


Turn static files into dynamic content formats.

CreateΒ aΒ flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.