pointer and array l.
Download
Skip this Video
Download Presentation
Pointer and Array 指標 與 陣列

Loading in 2 Seconds...

play fullscreen
1 / 15

Pointer and Array 指標 與 陣列 - PowerPoint PPT Presentation


  • 124 Views
  • Uploaded on

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).

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Pointer and Array 指標 與 陣列' - jimbo


Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
pointer and array

Pointer and Array指標 與 陣列

double *a, b[10];

char name[80], *np;

pointer
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

scanf format string list of pointers
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);

some considerations
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);

string character array
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

array is a pointer with fixed address
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

array of various types
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;

numeric array
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) 代表下一個變數、真正的位置增量看數的型態而定

array as an argument example
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

slide10
字串列印

char ss[40];

sprintf(ss,”This is a string.”); // store 17字元

printf(“output = %s\n”,ss);

============================

sprintf(buf, format, variables);

buf: 記憶體緩衝區 (字串位址) 。

format:字串,包含變數輸出格式 (與 printf 同)

variables:每一 % 格式所對應之變數。

array and pointer
Array and pointer
  • 陣列變數本身是一個指標變數 (pointer)
  • 陣列作為幅數傳遞時,傳遞的是位址 (pass by address)

與其他變數傳值不同 (pass by value) 。

  • 所以、在函數中改變陣列元素的值、會直接改變原呼叫函數中的陣列。
  • 當指標變數被賦予ㄧ位址之後,用法和陣列變數完全相同。
  • 如果指標指向超越陣列定義範圍,會產生 out of range 的錯誤,情況不穩定。
honer s method for polynomial
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

example

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 );

scheme

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

project 4 root of polynomial
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