Download presentation
Presentation is loading. Please wait.
Published byCornelia West Modified over 9 years ago
1
Computer Programming in C Chapter 5 2013 년 가을학기 부산대학교 전자전기정보컴퓨터공학부
2
STEMPNU Computer Programming Chapter 5 2 1. Pointer 와 Address Address 또는 Pointer Main Memory 의 위치를 지정 각 Variable 의 값을 저장하는 위치 &n : m 이 저장된 위치 (0x20AC) Pointer Variable Address 를 값으로 하는 변수 int n,m; n=20; m=30; Main memory n = 20 0x20A C int n,*pn; pn=&n; *pn=30; pn = 20AC
3
STEMPNU 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; } 와 비교
4
STEMPNU Computer Programming Chapter 5 4 3. Pointer 와 Array Pointer 와 Array 의 유사성과 차이점 intvalues[100]; values: values array 의 처음 주소 int*pvalues; pvalues=values; pvalues[0] 와 values[0] 은 서로 호환 가능 차이점 : values 는 상수 Main memory values[0] 0x20AC=values=&values[0] … values[99]
5
STEMPNU 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] */ Example intsum=0;intsum=0; int i, 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++; */ intsum=0; int i, values[10]; pvalues=values; for(i=0;i<10;i++) sum=sum+pvalues[i];
6
STEMPNU Computer Programming Chapter 5 6 4. Array Arithmetic Increment and Decrement pvalues 가 0x1000 일 때 pvalues++ 한 후 pvalues != 0x1000 pvalues == 0x1000 + sizeof(type of pvalues, int 의 경우 4) Multiplication 과 Division 은 Pointer 에 대하여 사용 불가능 int*pvalue; intvalue; pvalue=&value; pvalue=pvalue*2; /* Error */ pvalue=pvalue+2; /* OK */
7
STEMPNU 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”; voidstrcpy(char *s, char *d) { while((*s++=*d++)!=‘\0’); } OR voidstrcpy(char s[], char d[]) { inti=0; while((s[i]=d[i++])!=‘\0’); }
8
STEMPNU Computer Programming Chapter 5 8 5. Character Pointer Pointer to String and Double Pointer Example char*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[]) stringArray[0] stringArray[1] “Hello first” “Hello second” pstr
9
STEMPNU Computer Programming Chapter 5 9 6. Pointer Array and Multidimensional Array Pointer Array: Array of Pointer Example intvalues[10][20]; int*pvalues[10]; pvalues[0]=values[0]; pvalues[0]++; /* ? */ pvalues[0][1]; /* ? */ Main memory values[0][0] … … … values[0][1] values[1][0] values[9][19] values[0] = 0x1000 values[1] =0x1050 values =0x1000
10
STEMPNU Computer Programming Chapter 5 10 7. Double Pointer Example #include void main() { char *string = "Hello, world!"; char *copystr; if(allocstr(strlen(string), ©str))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?
11
STEMPNU Computer Programming Chapter 5 11 8. Pointer to Function Function Name An Address: “Pointer to Function” makes sense Example #include #include intfunctionA(int,int); intfunctionB(int,int); void main() { int (*pf)(int,int); pf=&functionB; printf("Result: %d\n",pf(10,20)); pf=&functionA; printf("Result: %d\n",pf(10,20)); } int functionA(int a,int b) { return a*b; } int functionB(int a,int b) { return a+b; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.