1 / 11

Computer Programming in C Chapter 5

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

giolla
Télécharger la présentation

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 2013 년 가을학기 부산대학교 전자전기정보컴퓨터공학부

  2. 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

  3. 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

  4. 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

  5. Pointer와 Array • Pointer와 Array Index intvalues[100], *pvalues;pvalues=values; /* pvalues[0]==values[0] */++pvalues; /* *pvalues==pvalues[1]==values[1] */ • Exampleint sum=0; int sum=0; inti, values[10]; inti,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+*pvalues++; */ int sum=0; inti, values[10]; pvalues=values;for(i=0;i<10;i++) sum=sum+pvalues[i]; Computer Programming Chapter 5

  6. 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

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

  8. 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

  9. 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

  10. 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

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

More Related