1 / 20

a[0]

Address:4 byte. pa. ppa. Double:8 byte. *ppa. *pa. a[0]. ????. a[1]. 動態配置記憶體. pointer 與陣列的比較 int a[5]; // 宣告五個整數的陣列 int *p; //p 為指向整數的指標,並無宣告其他空間 動態配置記憶體 #include <stdlib.h> p=malloc(5*sizeof(int)); // memory allocation free(p); // free memory. *p. *(p+1). p. …. *(p+4).

jerrod
Download Presentation

a[0]

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. Address:4 byte pa ppa Double:8 byte *ppa *pa a[0] ???? a[1]

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

  3. cast (unsigned char*) Example #include <stdio.h> #include <stdlib.h> #include <string.h> 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); }

  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. pa Example *pa “abc” *(pa+1) “123” #include <stdio.h> #include <stdlib.h> 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+2)

  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’; s[0][0] ncol s s[0][1] s[i][j] nrow

  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 */ ncol ncol s s[i][j] nrow nrow …

  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 …body…(hidden from user) output

  11. Example: Power • Function: Power(2,3) -> 23 #include <stdio.h> 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; } These variable names are local to this function Return-type function-name(parameter declarations…) { Declarations statements }

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

  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 <stdio.h> 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 memory address … Data segment • Store-program concept • Program is data Program segment main … Data segment Program segment power …

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

  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 <stdio.h> 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 <stdio.h> 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; }

More Related