1 / 22

10 장 포인터와 문자열

10 장 포인터와 문자열. 학습 목표. 포인터 기본 배열과 포인터 매개변수 전달방법 포인터와 문자열. 포인터 선언. 포인터의 선언 자료형 * 변수명; 선언 예 int *ptr;. 포인터 기본 예제. 프로그램 10-1. #include &lt;stdio.h&gt; int main(void) { int i = 10; int *ptr = &amp;i; printf(&quot;ptr 의 주소값 : %p<br>&quot;, &amp;ptr);

dara-burke
Download Presentation

10 장 포인터와 문자열

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 10 장 포인터와 문자열

  2. 학습 목표 • 포인터 기본 • 배열과 포인터 • 매개변수 전달방법 • 포인터와 문자열

  3. 포인터 선언 • 포인터의 선언 • 자료형 * 변수명; • 선언 예 • int *ptr;

  4. 포인터 기본 예제 프로그램 10-1 #include <stdio.h> int main(void) { int i = 10; int *ptr = &i; printf("ptr의 주소값 : %p\n", &ptr); printf("i의 주소값: %p\n", &i); printf("i의 주소값: %p\n\n", ptr); printf("i의 값: %d\n", i); printf("i의 값: %d\n", *ptr); return 0; } 실행 결과

  5. 포인터 기본 예제 프로그램 10-2 #include <stdio.h> int main(void) { int i = 10; int *ptr = &i; *ptr = i+20; printf("i의 값은: %d \n", i); i = i + 20; printf("i의 값은: %d \n", *ptr); return 0; } 실행 결과

  6. 지역변수의 한계

  7. 포인터를 사용하여 처리한 결과

  8. 지역 변수의 한계 프로그램 10-3 #include <stdio.h> void Swap(int a, int b); int main(void) { int x =10, y =5; printf("Swap 함수 실행 전 \n"); printf("x = %d, y = %d 이다.\n", x, y); Swap(x, y); printf("Swap 함수 실행 후 \n"); printf("x = %d, y = %d 이다.\n", x, y); return 0; } 실행 결과 void Swap(int a, int b) { int temp; temp = a; a = b; b = temp; }

  9. 지역 변수의 한계 프로그램 10-4 #include <stdio.h> void Swap(int* a, int* b); int main(void) { int x =10, y =5; printf("Swap 함수 실행 전 \n"); printf("x = %d, y = %d 이다.\n", x, y); Swap(&x, &y); printf("Swap 함수 실행 후 \n"); printf("x = %d, y = %d 이다.\n", x, y); return 0; } 실행 결과 void Swap(int* a, int* b) { int temp; temp = *a; *a = *b; *b = temp; }

  10. 배열과 포인터

  11. 배열과 포인터 예제 프로그램 10-5 #include <stdio.h> int main(void) { int a[] = { 10, 20, 30, 40, 50 }; int *p = a; printf("배열명 a를 이용한 주소 표현 \n"); for(i=0; i<5; i++) printf("a[%d]의 주소 %p\n", i, a + i); printf("포인터 p를 이용한 배열 주소 표현 \n"); for(i=0; i<5; i++) printf("a[%d]의 주소 %p\n", i, p + i); printf("포인터 p를 이용한 배열 값 표현 \n"); for(i=0; i<5; i++) printf("a[%d] = %d\n", i, *(p + i)); return 0; } 실행 결과

  12. 배열과 포인터 예제 프로그램 10-6 #include <stdio.h> int main(void) { int a[] = { 11, 22, 33, 44, 55 }; int *p = a; printf("*p의 값 = %d\n", *p); p++; printf("*(p+1)의 값 = %d\n", *p); p++; printf("*(p+2)의 값 = %d\n", *p); p--; printf("*(ptr+1)의 값 = %d\n", *p); return 0; } 실행 결과

  13. 포인터를 함수의 인자로 프로그램 10-7 #include <stdio.h> int SumArray(int* pA, int Size); int main(void) { int a[ ] = {10, 5, 15, 25, 7}; int Sum; Sum = SumArray(a, 5); printf("배열의 합 : %d \n", Sum); return 0; } int SumArray(int* pA, int Size) { int result = 0, i; for(i=0; i<Size; i++) result += pA[i]; // result += *(pA + i); // result += *pA++; return result; } 실행 결과

  14. 포인터를 함수의 인자로 프로그램 10-8 #include <stdio.h> void CountIncrement1(int n); // 값에 의한 전달 void CountIncrement2(int* n); // 주소에 의한 전달 int main(void) { int a = 10; printf("a의 초기값 : %d\n", a); CountIncrement1(a); printf("CountIncrement1 함수 실행 후 a의 값 : %d\n", a); CountIncrement2(&a); printf("CountIncrement2 함수 실행 후 a의 값 : %d\n", a); return 0; } void CountIncrement1(int n) { n++; } void CountIncrement2(int* n) { (*n)++; } 실행 결과

  15. 포인터를 이용한 문자열 표현 char *포인터 이름 = “ 문자열 ”; ex ) char *p = "MOON";

  16. 문자열 처리함수의 종류

  17. 문자열 처리 프로그램 10-9 #include <stdio.h> int main(void) { char *pC = "C programming"; printf("문자열을 변환 명세를 이용해서 출력\n"); printf("%s\n", pC); printf("문자를 반복해서 출력\n"); while(*pC) printf("%c", *pC++); printf("\n"); return 0; } 실행 결과

  18. 문자열 처리 프로그램 10-10 #include <stdio.h> int main(void) { char *pStr[ ] = {"english", "math", "korean"}; char subject[][8] = {"english", "math", "korean"}; int i; printf("포인터 배열을 이용해서 문자열을 출력합니다.\n"); for(i=0; i<3; i++) printf("pStr[%d] = %s\n", i, pStr[i]); printf(" 이차원 배열을 이용해서 문자열을 출력합니다.\n"); for(i=0; i<3; i++) printf("subject[%d] = %s\n", i, subject[i]); return 0; } 실행 결과

  19. 문자열 처리 프로그램 10-11 #include <stdio.h> int main(void) { char *pC = "c language"; while (*pC) { printf("%c", *pC-32); pC++; } printf("\n"); return 0; } 실행 결과

  20. 문자열 처리 프로그램 10-12 #include <stdio.h> #include <ctype.h> int main(void) { char *pC = "c language"; while (*pC) { printf("%c", toupper(*pC++)); } printf("\n"); return 0; } 실행 결과

  21. 문자열 처리 프로그램 10-13 #include <stdio.h> int strleng(char *ch); int main(void) { int len; char str[] = "c language and data structure"; len = strleng(str); printf("문자열의 크기는 %d 이다.\n", len); return 0; } int strleng(char *ch) { int index; for (index = 0; *(ch+index) != '\0'; index++); return index; } 실행 결과

  22. 문자열 처리 프로그램 10-14 #include <stdio.h> #include <string.h> int main(void) { char cmp1[40] = "C programming"; char cmp2[ ] = "Java programming"; char cmp3[ ] = "C programming"; char str[ ] = " is easy"; int length, i; length = strlen(cmp1); for(i=0 ; i < length; i++) printf("%c", cmp1[i]); printf("\n"); printf("cmp1과 cmp2는 서로 %s\n", strcmp(cmp1, cmp2) ? "같다" : "같지 않다"); printf("cmp1과 cmp3는 서로 %s\n", strcmp(cmp1, cmp3) ? "같다" : "같지 않다"); printf("cmp1에 cmp2를 복사하면 \"%s\"이 출력된다\n", strcpy(cmp1, cmp2)); printf("cmp1에 str를 연결하면 \"%s\"이 출력된다\n", strcat(cmp1, str)); return 0; } 실행 결과

More Related