1 / 15

Pointer and Array 指標 與 陣列

Pointer and Array 指標 與 陣列. double *a, b[10]; char name[80], *np;. Pointer 指標. 指標為正整數 ( 或長正整數 ) 變數,用來存放某特定數態的位址。 double a, c, *b; // a 是 8-byte 浮點實數, b 是 double 指標 b = &a; // 將 a 的位址存入 b c = *b; // 將 b 所指向的浮點實數存為 c. (*b).

jimbo
Download Presentation

Pointer and Array 指標 與 陣列

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. 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 (*b) 0.123456789012345 e+05 &a = 8076 &b 8076 &a &c = 8084 8-byte

  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 0 +1 +2 +3 +5 +8 +4 +6 c m y p r o g . c fname &achr \o …. +10 +9 +11 ap = &achr; ap = fname; ap = &fname[9] &ap

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

  9. Array as an argument (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. f(x) f’(x) x2 x1 牛頓法解根 (example) 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. main Scheme 1 input n, a[0]-a[n] 2 1 3,4 Int TwoDouble Read xinit input(a) poly(x,a,n) yes |xinit|>100? Read n end Horing method no 3,4 dx = df/f x = x - dx Read a[0]-a[n] Return f&df 5,6 no |dx|<xcrit ? 5,6 Return n yes Output x 2

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

More Related