1 / 12

Computer Programming in C Chapter 5

Computer Programming in C Chapter 5. 2004 년 가을학기 부산대학교 전자전기정보컴퓨터공학부. 5 장 . Pointer 와 Array. 목차 Pointer 와 주소 Pointer 와 Function Parameter Pointer 와 Array Array Arithmetic Character Pointer Pointer Array 와 Multidimensional Array Double Pointer Pointer to Function.

phoebe
Download Presentation

Computer Programming in C Chapter 5

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. Computer Programming in C Chapter 5 2004년 가을학기 부산대학교 전자전기정보컴퓨터공학부

  2. 5장. Pointer와 Array • 목차 • Pointer와 주소 • Pointer와 Function Parameter • Pointer와 Array • Array Arithmetic • Character Pointer • Pointer Array와 Multidimensional Array • Double Pointer • Pointer to Function Computer Programming Chapter 5

  3. 1. Pointer 와 Address • Address 또는 Pointer • Main Memory의 위치를 지정 • 각 Variable의 값을 저장하는 위치 • &n : m이 저장된 위치 (0x20AC) • Pointer Variable • Address를 값으로 하는 변수 Main memory int n,m;n=20;m=30; 0x20AC n = 20 pn = 20AC int n,*pn;pn=&n;*pn=30; Computer Programming Chapter 5

  4. 2. Pointer와 Function Parameter • Function의 Parameter • Call-By Value를 통한 Function내의 값 변경 int m,n; . . . m=20; m=50;swap(&m,&n); . . . void swap(int *a, int *b) { int m; m=*a; *a=*b; *b=m; } int m,n; . . . m=20; m=50;swap(m,n); . . . void swap(int a, int b) { int m; m=a; a=b; b=m; } 와 비교 Computer Programming Chapter 5

  5. 3. Pointer와 Array Main memory • Pointer 와 Array의 유사성과 차이점 • int values[100]; • values: values array의 처음 주소 • int *pvalues;pvalues=values; pvalues[0]와 values[0]은 서로 호환 가능 • 차이점: values는 상수 0x20AC=values=&values[0] values[0] … values[99] Computer Programming Chapter 5

  6. Pointer와 Array • Pointer와 Array Index int *pvalues;pvalues=values; /* pvalues[0]==values[0] */++pvalues; /* *pvalues==values[1] */ • Exampleint sum=0; int sum=0; int i, values[10]; int i,values[10],*pvalues;/* ... value[0], ... value[9] */ pvalues=values;for(i=0;i<10;i++) for(i=0;i<10;i++) sum=sum+values[i]; sum=sum+*values++; */ int sum=0; int sum=0; int i, values[10]; int i,values[10],*pvalues;pvalues=values;for(i=0;i<10;i++) sum=sum+pvalues[i]; Computer Programming Chapter 5

  7. 4. Array Arithmetic • Increment and Decrement • pvalues가 0x1000일 때 pvalues++ 한 후pvalues != 0x1000pvalues == 0x1000 + sizeof(type of pvalues, int의 경우 4) • Multiplication과 Division은 Pointer에 대하여 사용 불가능 int *pvalue; int value; pvalue=&value; pvalue=pvalue*2; /* Error */ pvalue=pvalue+2; /* OK */ Computer Programming Chapter 5

  8. 5. Character Pointer • Character Pointer • Exchangeable with Character Array • Example char *string1=“Hello World\n”;char string2[]=“Hello World\n”; Computer Programming Chapter 5

  9. 5. Character Pointer • Pointer to String and Double Pointer • Examplechar *stringArray[2]; char **pstr; stringArray[0]="Hello first\n"; stringArray[1]="Hello second\n"; pstr=stringArray; printf("%s",*pstr++); printf("%s",*pstr); • Parameters of main function void main(int argc, char *argv[]) pstr “Hello first” stringArray[0] “Hello second” stringArray[1] Computer Programming Chapter 5

  10. 6. Pointer Array and Multidimensional Array • Pointer Array: Array of Pointer • Exampleint values[10][20];int *pvalues[10];pvalues[0]=values[0]; pvalues[0]++; /* ? */ pvalues[0][1]; /* ? */ Main memory values=0x1000 values[0]= 0x1000 values[0][0] values[0][0] values[0][1] … values[1]=0x1050 values[1][0] … … values[9][19] Computer Programming Chapter 5

  11. 7. Double Pointer • Example #include <stdlib.h> void main() { char *string = "Hello, world!"; char *copystr; if(allocstr(strlen(string), &copystr))strcpy(copystr, string); else fprintf(stderr, "out of memory\n"); ...}int allocstr(int len, char **retptr) { char *p = malloc(len + 1); /* +1 for \0 */ if(p == NULL) return 0; *retptr = p; return 1; } • If single pointer would be used? Computer Programming Chapter 5

  12. 8. Pointer to Function • Function Name • An Address: “Pointer to Function” makes sense • Example Computer Programming Chapter 5

More Related