1 / 42

제 7 장 배열과 포인터

제 7 장 배열과 포인터. Hello!! C 언어 강성호 김학배 최우영. 순서. 배열 포인터 배열과 포인터의 관계 문자열 요약. 배열. 배열의 정의 동일한 이름과 동일한 테이터형을 가진 여러개의 변수들을 동시에 저장하는 영역 배열 요소의 정의 각각의 저장 영역 배열의 이름은 첫 번째 요소의 저장 번지를 나타낸다. 배열. 일차원 배열 첨자 ([] 안의 숫자 ) 가 하나인 배열 형식 < 형식 > 변수형 배열 이름 [ 배열요소개수 ]; int array[10]; 이차원 배열

Download Presentation

제 7 장 배열과 포인터

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. 제 7장 배열과 포인터 Hello!! C 언어 강성호 김학배 최우영

  2. 순서 제 7장 배열과 포인터 • 배열 • 포인터 • 배열과 포인터의 관계 • 문자열 • 요약

  3. 배열 제 7장 배열과 포인터 • 배열의 정의 • 동일한 이름과 동일한 테이터형을 가진 여러개의 변수들을 동시에 저장하는 영역 • 배열 요소의 정의 • 각각의 저장 영역 • 배열의 이름은 첫 번째 요소의 저장 번지를 나타낸다.

  4. 배열 제 7장 배열과 포인터 • 일차원 배열 • 첨자([]안의 숫자)가 하나인 배열 • 형식 • <형식> 변수형 배열 이름 [배열요소개수]; int array[10]; • 이차원 배열 • 두 가지 종류의 첨자가 필요한 배열 • 형식 • <형식> 변수형 배열명[첫 번째 배열요소][두 번째 배열요소개수]; int array [3] [5];

  5. 배열의 초기화 제 7장 배열과 포인터 • 배열의 초기화 형식 • <형식> 변수형 배열명[배열요소개수]={ x1,…,x10} (초기화값); • 개수가 같아야 함 • 일차원 배열의 초기화 • 숫자형 배열의 초기화 • 쉼표(,)로 구분한다. • 중괄호({})로 묶어 준다. • 예 : int array[10]={1,2,3,4,5,6,7,8,9,10}; • 문자열 배열의 초기화 • 큰따옴표(" ")로 묶어 준다. • 예 : char array[7]="Turbo-C";

  6. 배열의 초기화 제 7장 배열과 포인터 • 이차원 배열의 초기화 • 숫자 배열의 초기화 int array[2][3]={{1,2},{3,4},{5,6}}; • 문자열 배열의 초기화 int array[3][10]={"Turbo-C","is","easy!"}; • 함수 인자로서의 배열 • 함수의 인자로서 배열의 이름을 사용 • 함수에 배열의 첫 번째 주소값이 전달

  7. 배열의 초기화 제 7장 배열과 포인터 [예제 7-1] #include <stdio.h> #define MAX 10 int prn(int x[MAX]); main(){ int num[MAX] = {1,2,3,4,5,6,7,8,9,10}; prn(&num[0]); /*첫 번째 배열 요소의 주소를 prn함수에 전해줌 */ } int prn(int x[MAX]){ int i; printf("The address of array name is %d \n", x); for ( i = 0 ; i < MAX ; i++ ){ printf("ADDRESS : %d\t", &x[i]); printf("VALUE : %d\n", x[i]); } } 계속

  8. 배열의 초기화 제 7장 배열과 포인터 [예제 7-1] 계속 [실행결과] The address of array name is 404 ADDRESS : 4076 VALUE : 1 ADDRESS : 4078 VALUE : 2 ADDRESS : 4080 VALUE : 3 : : : : ADDRESS : 4094 VALUE : 10

  9. 포인터 제 7장 배열과 포인터 • 포인터의 선언 및 간접 연산자 • 포인터의 개념 • 모든 변수는 각각의 주소값을 갖는다. • 변수 pnt에 다른 변수 a의 주소를 저장했을 때, pnt를 a에 대한 포인터라 한다. • 포인터의 선언 • 변수형 *변수명;

  10. 포인터 제 7장 배열과 포인터 [예제 7-2] #include<stdio.h> main() { int a = 50; int *pnt; printf("a=%d pnt=%d\n ",a,pnt); } [실행결과] a=50 pnt=1824

  11. 포인터 제 7장 배열과 포인터 [예제 7-3] #include<stdio.h> main() { int a = 50; /* 먼저 변수를 초기화한 후 */ int *pnt; /* 포인터를 선언 */ pnt = &a; /* 참조된 변수의 주소값으로 초기화 */ printf("a=%d pnt=%d \n",a,pnt); } [실행결과] a=50 pnt=4094

  12. 포인터 제 7장 배열과 포인터 • 포인터 연산 • 예 : int_pnt2=int_pnt1+1; • int_pnt1이 의미하는 주소값을 한 단위 (int==> 2byte)증가시켜 바로 다음 위치의 주소값을 int_pnt2에 저장 • 포인터의 주소 계산법 • 포인터 변수의 `형'에 따라 다름 int_pnt++ ==> 2byte씩 증가 float_pnt++ ==> 4byte씩 증가

  13. 포인터 제 7장 배열과 포인터 [예제 7-4] #include <stdio.h> #define MAX 10 int i_array[MAX] = {1, 2, 3, 4, 5, 6, 7, 8, 9,10}; /* 배열선언 및 초기화*/ int I; int *i_pnt; /* 포인터 선언 */ float f_array[MAX] = {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0}; /* 배열선언 및 초기화*/ float *f_pnt; /* 포인터 선언*/ main(){ i_pnt = i_array; /* 포인터 초기화 */ f_pnt = f_array; /* 포인터 초기화 */ [실행결과] for ( i=0 ; i<MAX ; i++) 1 0.100000 printf("%d\t%f\n",*i_pnt++,*f_pnt++); 2 0.200000 } 3 0.300000 : : 10 1.000000

  14. 배열과 포인터의 관계 제 7장 배열과 포인터 • 포인터명과 배열명은 메모리의 주소 • 포인터 연산자(*)와 배열 연산자([])의 의미 • *(pnt+i) : 주소 pnt 이후의 i번째 데이터를 구한다. • array[i] : 주소 array 이후의 I번째 데이터를 구한다. • 포인터 배열 • 길이가 가변적인 문자열 데이터를 배열로 처리할 때 • 함수 호출시 다수의 포인터를 인도할 때 • 포인터 배열과 메모리 • 예 : char *pnt[3]={`KIM',`LEE',`PARK'}; • 포인터 배열 pnt 의 영역에 각 문자열의 시작주소가 수록 • 이차원 배열과 메모리 • 각 배열 요소에 대한 메모리 영역이 모두 확보 • 포인터와 메모리 할당 • malloc(), calloc() 함수 이용

  15. 배열과 포인터의 관계 제 7장 배열과 포인터 [예제 7-5] #include<stdio.h> main(){ int a[5]={1,2,3,4}; int *pnt; pnt = a; printf("%d %d %d %d \n",*pnt,*(pnt+1),*(pnt+2),*(pnt+3)); printf("%d %d %d %d \n",a[0],a[1],a[2],a[3]) ; } [실행결과] 1 2 3 4 1 2 3 4 /* 두 실행 결과는 같음 */

  16. 배열과 포인터의 관계 제 7장 배열과 포인터 [예제 7-6] #include<stdio.h> main(){ int a[2][2] = { 1,2, 3,4 }; int *pnt; pnt=a[0]; printf("%d %d\n%d %d\n",*pnt,*(pnt+1),*(pnt+1*2),*(pnt+1*2+1)); printf("%d %d\n%d %d\n",a[0][0],a[0][1],a[1][0],a[1][1]); } [실행결과] 1 2 3 4 1 2 /* 두 실행결과는 같음 */ 3 4

  17. 배열과 포인터의 관계 제 7장 배열과 포인터 [예제 7-7] #include<stdio.h> main() { int a[4] = {1, 2, 3, 4}; int *pnt; pnt=a; printf("%d %d %d %d \n",pnt[0],pnt[1],pnt[2],pnt[3]); } [실행결과] 1 2 3 4

  18. 배열과 포인터의 관계 제 7장 배열과 포인터 • main 함수의 인자 • 두 가지의 적합한 형태 int main() int main( argc, argv ) int argc; char **argv; • 포인터의 포인터 • 포인터 변수를 가리키는 포인터 char **string_array={"Dog","Cat","Lion","Tiger"}; • void 포인터 • 포인터의 데이터형이 정해지지 않은 포인터 • 데이터형에 관계없이 동작하는 함수를 제작하는데 유용 • 함수 포인터 • 코드를 가리키는 포인터

  19. 포인터 배열 제 7장 배열과 포인터 • 포인터를 배열로 만든 것 • 선언 변수형 *변수명[배열요소개수]; 예) int *pnt+array [20]; • 선언과 동시에 초기화 변수형 *변수명[배열요소개수] = {인자1, 인자2, … 인자n} ; 예) char *pnt[3] = {“pointer",“string",“pointerarray"};

  20. 배열과 포인터의 관계 제 7장 배열과 포인터 [예제 7-8] #include<stdio.h> void main() { char *pnt[] = {"Chan","Ho","Park"}; /* 포인터 배열 선언 */ char array[3][5]= {"Chan","Ho","Park"}; /* 이차원 배열 선언 */ int j; printf("memory address of pointer array pnt\n"); printf("pnt => %p\n",pnt); for(j=0;j<3;j++) printf("pnt[%d] (address %p) => %s\n",j,pnt[j],pnt[j]); printf("\nmemory address of two-dimensional array\n"); printf("array => %p\n",array); for(j=0;j<3;j++) printf("array[%d] (address %p) => %s\n",j,array[j], array[j]); } 계속

  21. 배열과 포인터의 관계 제 7장 배열과 포인터 [예제 7-8] 계속 [실행결과] memory address of pointer array pnt pnt => 1747:0FF4 pnt[0] (address 16FB:00AF) => Chan pnt[1] (address 16FB:00B4) => Ho pnt[2] (address 16FB:00B7) => Park memory address of two-dimensional array array => 1747:0FE4 array[0] (address 1747:0FE4) => Chan array[1] (address 1747:0FE9) => Ho array[2] (address 1747:0FEE) => Park

  22. 배열과 포인터의 관계 제 7장 배열과 포인터 • 포인터의 포인터 • 포인터 변수를 가리키는 포인터 char **string_array={"Dog","Cat","Lion","Tiger"}; • 다중간접참조(indirection)

  23. 배열과 포인터의 관계 제 7장 배열과 포인터 [예제 7-9] #include<stdio.h> main() { int x; int *pnt=&x; /* pnt는 x에 대한 포인터 */ int **p_pnt=&pnt; /* p_pnt는 pnt에 대한 포인터 */ **p_pnt=100; /* 변수 x에 100을 할당 */ printf("x=%d\n",x); } [실행결과] x=100

  24. 배열과 포인터의 관계 제 7장 배열과 포인터 • void 포인터 • 포인터의 데이터형이 정해지지 않은 포인터 • 데이터형에 관계없이 동작하는 함수를 제작하는데 유용 • 함수 포인터 • 코드를 가리키는 포인터

  25. 문자열 제 7장 배열과 포인터 • 문자열의 개념 • 문자 상수 • ASCII 코드화 • char형 선언 • 문자열 상수 • char형 배열로 선언 • 문자열 배열과 초기화 • 문자 개수보다 하나 더 많은 배열 요소를 선택 char array[6]="Seoul";

  26. 문자열 제 7장 배열과 포인터 [예제 7-10] /* 문자 상수 사용의 예*/ #include <stdio.h> main() { char ch = 'a'; char num = '2'; printf("ch=%c, num=%c\n",ch,num); } [실행결과] ch=a, num=2

  27. 문자열과 포인터의 관계 제 7장 배열과 포인터 • 포인터와 문자열 • 배열명은 첫 번째 요소에 대한 포인터 char *pnt="hello"; • pnt에는 첫문자인 `h'의 저장 번지가 들어있다. char pnt[]="hello"; ==> • 배열명이 첫 문자인 `h의저장번지를 가리키는 포인터가 된다.

  28. 문자열의 입출력 제 7장 배열과 포인터 • 문자열 입출력 함수 • gets(), scanf() • stdio.h 헤더화일 포함 • gets() 함수의 형식 • scanf() 함수의 형식

  29. 문자열의 입출력 제 7장 배열과 포인터 [예제 7-11] #include <stdio.h> main() { char array[20]; printf("Enter a string : "); gets(array); printf("The received string : %s\n", array); } [실행결과] Enter a string : I love you. The received string : I love you.

  30. 문자열의 입출력 제 7장 배열과 포인터 [예제 7-12] #include <stdio.h> main() { char season[10]; char month[10]; printf("Enter season and month\n"); scanf("%s%s", season , month); printf("season = %s , month = %s", season , month); } [실행결과] Enter season and month spring May season = spring , month = May

  31. 문자열의 입출력 제 7장 배열과 포인터 • 문자열 입출력 함수 • puts(), prinf() • stdio.h 헤더파일 포함 • puts()함수의 형식 • printf()함수의 형식

  32. 문자열의 입출력 제 7장 배열과 포인터 [예제 7-13] /* Example using puts() library function */ #include <stdio.h> main(){ char *a="How"; char *b="old"; char string1[]="are"; char string2[]="you!"; puts(a); puts(b); puts(string1); puts(string2); [실행결과] } How old are you!

  33. 문자열의 입출력 제 7장 배열과 포인터 [예제 7-14] /* Example using printf() library function */ #include <stdio.h> main(){ char f_name[30] = "Michael"; char l_name[30] = "Jordan"; printf("What`s your name?\n"); printf("f_name : %s\n", f_name); printf("l_name : %s\n", l_name); } [실행결과] What`s your name? f_name : Michael l_name : Jordan

  34. 문자열의 처리 제 7장 배열과 포인터 • 문자열 라이브러리 함수 • string.h 헤더파일 포함 • 문자열의 길이, strlen() • 문자열의 복사, strcpy() • 문자열의 결합, strcat()

  35. 문자열의 처리 제 7장 배열과 포인터 [예제 7-15] /* Program for an example of string library functions */ #include <stdio.h> #include <string.h> main(){ char string[30]; int a; printf("Enter a string : "); gets(string); a=strlen(string); printf("The length of string is %d.",a); } [실행결과] Enter a string : abcdefg The length of string is 7.

  36. 문자열의 처리 제 7장 배열과 포인터 [예제 7-16] /* Example using strcpy() function */ #include <stdio.h> #include <string.h> main(){ char str1[80],str2[80],*str3; printf("Enter string1 : "); gets(str1); printf("Enter string2 : "); gets(str2); str3=strcpy(str1,str2); printf("%s is copied in str1\n",str3); } [실행결과] Enter string1 : Korea Enter string2 : aeroK aeroK is copied in str1

  37. 문자열의 처리 제 7장 배열과 포인터 [예제 7-17] /* Example using strcat() library function */ #include <stdio.h> #include <string.h> main(){ char dest[80]; char sour[80]; gets(dest); printf("This string, [%s] is in dest.\n" , dest); gets(sour); strcat(dest, sour); printf("This string, [%s] is in sour.\n" , sour); printf("This string, [%s] is in dest." , dest); } 계속

  38. 문자열의 처리 제 7장 배열과 포인터 [예제 7-17] 계속 [실행결과] Hi, This string, [Hi, ] is in dest. everyone. This string, [everyone.] is in sour. This string, [Hi, everyone.] is in dest.

  39. 문자열의 처리 제 7장 배열과 포인터 • 문자열 라이브러리 함수 • 문자열의 검색, strchr() • 문자열 변환, strlwr()와 strupr()

  40. 문자열의 처리 제 7장 배열과 포인터 [예제 7-18] /* Example using srtchr() library function */ #include <stdio.h> #include <string.h> void main(void){ char a[80],*d; int c; printf("Enter a string : "); gets(a); printf("The character to be searched : "); c=getchar(); d=strchr(a,c); printf("The character you are searching,"); printf("'%c'is discovered %d position in a.",c,d-a+1); } 계속

  41. 문자열의 처리 제 7장 배열과 포인터 [예제 7-18] 계속 [실행결과] Enter a string : beautiful The character to be searched : l The character you are searching,'l'is discovered 9 position in a.

  42. 문자열의 처리 제 7장 배열과 포인터 [예제 7-19] /* Example using strlwr() and strupr() */ #include <stdio.h> #include <ctype.h> main(){ char a,b,c; char ch; printf("Enter alphabet : "); scanf("%c", &ch); b = tolower( ch ); printf("Convert into lower case letter : %c\n",b); c = toupper( ch ); printf("Convert into upper case letter : %c\n",c); } [실행결과] Enter alphabet : a Convert into lower case letter : a Convert into upper case letter : A

More Related