Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointer and Array 指標 與 陣列 double *a, b[10]; char name[80], *np;

Similar presentations


Presentation on theme: "Pointer and Array 指標 與 陣列 double *a, b[10]; char name[80], *np;"— Presentation transcript:

1 Pointer and Array 指標 與 陣列 double *a, b[10]; char name[80], *np;

2 Pointer 指標 指標為正整數 ( 或長正整數 ) 變數,用來存放某特定數態 的位址。 double a, c, *b; // a 是 8-byte 浮點實數, b 是 double 指標 b = &a; // 將 a 的位址存入 b c = *b; // 將 b 所指向的浮點實數存為 c 8076 0.123456789012345 e+05 &a = 8076 &c = 8084 &b 8-byte (*b) &a

3 Scanf( “ format string ”, list of pointers) int a, *ap; double b, *bp; ap = &a; bp = &b; scanf(“%d %lf”, ap, bp); // same as scanf(“%d %lf”, &a, &b); printf(“a=%d b=%lf\n”, *ap, *bp); // same as printf(“a=%d b=%lf\n”, a, b);

4 Some Considerations double a=5.0, *ap; ap = &a; =============================== *(&a) = ? &(*ap) = ? &ap = ? printf(“ a = %lf\n”, *ap); printf(“ap = %p\n”,ap); printf(“ %p\n”,&ap); printf(“ap = %lx\n”,ap);

5 String = Character Array 字串 char fname[80], achr = ’c’, *ap; scanf(“%s”, fname); // fname =“myprog.c” ap = &achr; printf(“%c\n”,*ap); // c ap = fname; printf(“%c\n”, *ap); // m printf(“%c\n”, *(ap+1)); // y printf(“%c\n”,*(ap+2)); // p ……… example

6 Array is a pointer with fixed address c &achr myprog.c \o fname &ap 0+1+2+3 +4 +5 +6 +8 ap = &achr; ap = fname; ap = &fname[9] +9 +10 +11 ….

7 Array of various types char sss[80]; int na[20]; float farry[30]; struct {int n; double x;} sttt[5]; sttt[3].n = 1; sttt[3].x = 8.9;

8 Numeric Array float X[6]; X[0] X[1] X[2] X[3] X[4] X[5] X X+1 X+2 X+3 X+4 X+5 0 -- 3 4 -- 7 8 -- 11 12 -- 15 16 -- 19 20 -- 23 value pointer location X[3] = 5; *(X+3) = 5; X+3; &X[3] ※ (x+1) 代表下一個變數、真正的位置增量看數的型態而定

9 Array as an argument (example)example double funsum(int, double *, char *); int main() { int k, n=10; double xn[30], sum; char ss[40]; for (k = 0; k<n; k++) xn[k] = 1.0/ (double)(k+1)); sprintf(ss, “pass array dim(%d)”, n); sum = funsum(n, xn, ss); …. return 0; } double funsum(int nt, double *x, char *s) { int k; double summ; printf(“%s\n”,s); for (k=0; k<n; k++) summ = summ + x[k]; return(summ); } 結果 : Pass array dim(10) Sum = 0.909091

10 字串列印 char ss[40]; sprintf(ss,”This is a string.”); // store 17 字元 printf(“output = %s\n”,ss); ============================ sprintf(buf, format, variables); buf: 記憶體緩衝區 ( 字串位址 ) 。 format :字串,包含變數輸出格式 ( 與 printf 同 ) variables :每一 % 格式所對應之變數。

11 Array and pointer 陣列變數本身是一個指標變數 (pointer) 陣列作為幅數傳遞時,傳遞的是位址 (pass by address) 與其他變數傳值不同 (pass by value) 。 所以、在函數中改變陣列元素的值、會直接改變原呼叫函數中的 陣列。 當指標變數被賦予ㄧ位址之後,用法和陣列變數完全相同。 如果指標指向超越陣列定義範圍,會產生 out of range 的錯誤, 情況不穩定。

12 Honer ’ s method for Polynomial fx = a(n); dfx = 0.0; for (k=n ; k>0 ; k--) { dfx = dfx * x + fx; fx = fx * x + a[k-1]; } Notice: dfx 要先於 fx

13 牛頓法解根 (example)example f(x) f’(x) x1x2 x1 = xinit; do { fdf = poly(x, a, n); dx = fdf.x(x, a, n)/fdf.y(x, a, n); x1 -= dx; } while ( fabs(dx) > xcrit );

14 Scheme input(a) Read n Read a[0]-a[n] Return n poly(x,a,n) Horing method Return f&df 1 3,4 2 5,6 main input n, a[0]-a[n] dx = df/f x = x - dx |dx|<xcrit ? end 2 3,4 5,6 1 no yes Read xinit |xinit|>100? Output x yes no IntTwoDouble

15 Project. 4 Root of polynomial Using Newton’s method found the root of f(x) = x 5 + 2x 4 + 4x 3 + 8x 2 + 3x + 6 with xcrit = 10 -8 xinit = 0.0


Download ppt "Pointer and Array 指標 與 陣列 double *a, b[10]; char name[80], *np;"

Similar presentations


Ads by Google