200 likes | 329 Views
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).
E N D
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)
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); }
Quiz about your homework • Modify your homework such that it dynamically allocate a 2-d array
pointer to pointer array • 用來儲存不定長度的二維陣列資料 • Ex. 文字資料 a b c \0 1 2 3 4 5 6 7 8 \0 X \0 … • 預先宣告一固定大小陣列太浪費記憶體空間
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)
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
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 …
Why Functions? 副程式、函數 • Ex. printf(), scanf(), getchar(), … • Break large program into smaller ones • Call functions that others have done • Hide details of operations
Format of functions • Function, subroutine, procedure • printf, getchar, putchar, … input …body…(hidden from user) output
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);
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 }
Functions (cont.) arguments(formal arguments) power(2,i) parameters(actual arguments) power(int base, int n) …body…(hidden from user) return p;
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
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; }
Call by value memory address … Data segment • Store-program concept • Program is data Program segment main … Data segment Program segment power …
Call by value memory address … 2 i power(2,i) main Program segment 傳值過去 … base=2; n=i; base n Program segment power …
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; }
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; }
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; }