Presentation is loading. Please wait.

Presentation is loading. Please wait.

A[0] a[1] pa ???? *pa ppa *ppa Address:4 byte Double:8 byte.

Similar presentations


Presentation on theme: "A[0] a[1] pa ???? *pa ppa *ppa Address:4 byte Double:8 byte."— Presentation transcript:

1 a[0] a[1] pa ???? *pa ppa *ppa Address:4 byte Double:8 byte

2 動態配置記憶體 pointer 與陣列的比較 int a[5]; // 宣告五個整數的陣列 int *p; //p 為指向整數的指標,並無宣告其他空間 動態配置記憶體 #include p=malloc(5*sizeof(int)); // memory allocation free(p); // free memory p *p *(p+1) … *(p+4)

3 Example #include main() { unsigned char *get_block, *pointer; pointer="TEST!"; // allocate space to pointer? no get_block = (unsigned char *)malloc(sizeof(char)*6); strcpy(get_block, pointer); *get_block = 't'; get_block[2] = 's'; printf("%s\n", pointer); printf("%s\n", get_block); free(get_block); } cast (unsigned char*)

4 Quiz about your homework Modify your homework such that it dynamically allocate a 2-d array

5 pointer to pointer array 用來儲存不定長度的二維陣列資料 Ex. 文字資料 a b c \0 1 2 3 4 5 6 7 8 \0 X \0 … 預先宣告一固定大小陣列太浪費記憶體空間

6 Example #include main() { char **pa; pa = (char **)malloc(3*sizeof(char *)); *(pa) = "abc"; *(pa+1) = (char *)malloc(6*sizeof(char)); *(*(pa+1)+0)='0'; *(*(pa+1)+1)='1'; *(*(pa+1)+2)='2'; *(*(pa+1)+3)='\0'; } pa *pa *(pa+1) *(pa+2) “ abc ” “ 123 ”

7 Two ways to dynamically allocate multi-dim arrays (1) 1. char *s; s = (char *)malloc(sizeof(char)*nrow*ncol); *(s+i*ncol+j) = ‘ a ’ ; /* s[i][j] = ‘ a ’ ; */ *(s+i+j*nrow) = ‘ a ’ ; nrow ncol s s[i][j] s[0][0] s[0][1]

8 Two ways to dynamically allocate multi-dim arrays (2) 1. char **s; s = (char **)malloc(sizeof(char *)*nrow); for (i=0; i<nrow; i++) s[i] = (char *)malloc(sizeof(char)*ncol); s[i][j]= ‘ a ’ ; /* access data */ nrow ncol nrow ncol … s s[i][j]

9 Why Functions? 副程式、函數 Ex. printf(), scanf(), getchar(), … Break large program into smaller ones Call functions that others have done Hide details of operations

10 Format of functions Function, subroutine, procedure printf, getchar, putchar, … input output … body … (hidden from user)

11 Example: Power Function: Power(2,3) -> 2 3 #include int power(int m, int n); main() { int i; for(i=0; i<10; ++i) printf("%d %d\n", i, power(2,i)); } function prototype c.f. function definition int power(int, int); 函數原型宣告

12 int power(int base, int n) { int i, p; p = 1; for(i=1; i<=n; ++i) p = p*base; return p; } Return-type function-name(parameter declarations … ) { Declarations statements } These variable names are local to this function

13 Functions (cont.) power(int base, int n) return p; … body … (hidden from user) power(2,i) arguments(formal arguments) parameters(actual arguments)

14 Function definitions return-type function-name (argument declarations) { declarations and statements … return expression } * If return-type is omitted, int is assumed * Functions can occur in any order in the source file

15 Example: prog7-1.c #include double half(double); void main(void) { double r; int i; for(i=0; i<5; i++){ r = half( (double) i); printf("full=%d half=%f\n", i, r); } double half(double s) { return s/2; }

16 Call by value Store-program concept Program is data Data segment Program segment … … memory Data segment Program segment main power … address

17 2i2i Program segment … … memory Program segment main power … address power(2,i) base n Call by value 傳值過去 base=2; n=i;

18 Call by value - example int power(int base, int n) { int p; for(p=1; n>0; --n) p = p * base; return p; } int power(int base, int n) { int i, p; p = 1; for(i=1; i<=n; ++i) p = p*base; return p; }

19 swap 寫一函數,會交換兩個輸入變數的值 #include void swap(int, int); main() { int a=5, b=6; pirntf( “ a=%d, b=%d\n ”, a, b); swap(a, b); printf( “ a=%d, b=%d\n ”, a, b); } void swap(int c, int d) { int i; i=c; c=d; d=i; }

20 swap #include void swap(int *, int *); main() { int a=5, b=6; pirntf( “ a=%d, b=%d\n ”, a, b); swap(&a, &b); printf( “ a=%d, b=%d\n ”, a, b); } void swap(int *c, int *d) { int i; i = *c; *c = *d; *d = i; }


Download ppt "A[0] a[1] pa ???? *pa ppa *ppa Address:4 byte Double:8 byte."

Similar presentations


Ads by Google