Presentation is loading. Please wait.

Presentation is loading. Please wait.

計算機程式 第七單元 Pointers and Strings 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program,

Similar presentations


Presentation on theme: "計算機程式 第七單元 Pointers and Strings 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program,"— Presentation transcript:

1 計算機程式 第七單元 Pointers and Strings 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e, Harvey M. Deitel and Paul J. Deitel, both from Deitel & Associates, Inc. © 2010 。 本 講義僅引用部分內容,請讀者自行準備。 1 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法 第 46 、 52 、 65 條合理使用。 Microsoft 服務合約

2 Pointer Variables int a=12; int *aptr; double *bptr, cptr; aptr = &a; int n=3, *nptr = &n; *aptr = *nptr + 5; a = n + 5; Q: &*aptr = *&aptr??? 2 Note: A pointer can only be assigned to 0, NULL, and an address. 12 aptr a

3 * and & Operators 3 OperatorsAssociativityType () []left to righthighest ++ -- static_cast ( operand )left to rightunary (postfix) ++ -- + - ! & *right to leftunary (prefix) * / %left to rightmultiplicative + -left to rightadditive >left to rightinsertion/extraction >=left to rightrelational == !=left to rightequality &&left to rightlogical AND ||left to rightlogical OR ?:right to leftconditional = += -= *= /= %=right to leftassignment,left to rightcomma

4 Call-by-Reference with Pointer Fig. 8.7 (p. 352) vs. Fig. 6.18 (p.226) 4

5 5 p.351

6 6 p.352

7 Using const with Pointer Non-constant pointer to non-constant data o int *aptr; Fig. 7.4, p. 329 Non-constant point to constant data o const int *aptr; Fig. 7.10, p. 336 Constant pointer to non-constant data o int * const aptr = &a; Fig. 7.11, p. 337 Constant pointer to constant data o const int * const aprt = &a; Fig. 7.12, p. 338 7

8 Pointer vs. Array Constant pointer to non-constant data o e.g., int a[20], *aptr; aptr = a; aptr = &a[0]; Passing array into a function, o Fig. 8.13 (p. 360) vs. Fig. 7.13 (p. 301) o int * b vs. int b[] 8

9 Pointer vs. Array (cont.) Pointer arithmetic o int a[10], *aptr =a; aptr++; aptr += 3; Notation o Pointer/offset notation a[3] = *(a+3), *(aptr+3) &a[3] = a+3, aptr + 3 o Pointer/subscript notation a[3] = aptr[3] 9

10 10 p.359-361

11 Note: sizeof 11 p.363-364

12 Pointer vs. String char color[] = “blue”; char color[] = {‘b’, ‘l’, ‘u’, ‘e’, ‘\0’}; const char *colorptr=“blue”; char *c = &color[0]; cin >> color; cout<<color; cin>>setw(5)>>color; cin.getline(sentence, 80, ‘\n’); cin.get(); 12

13 Function Pointer A pointer to a function contains the function’s address in memory. Pointers to functions can be o Passed to functions o Returned from functions o Stored in arrays o Assigned to other function pointers o Used to call the underlying function 13

14 Function Pointer (cont.) 14 p.374-376

15 版權聲明 15 頁碼作品版權圖示來源 / 作者 1-15 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法第 46 、 52 、 65 條合理使用。 2 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.348 。 依據著作權法第 46 、 52 、 65 條合理使用。 3 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.350 。 依據著作權法第 46 、 52 、 65 條合理使用。 5-6, 10-11, 14 Open Clip Art Library ,作者: aritztg ,本作品轉載自: http://openclipart.org/detail/3422/mouse-by-aritztg ,瀏覽日期: 2013/1/10 。 http://openclipart.org/detail/3422/mouse-by-aritztg 12 Open Clip Art Library ,作者: Chrisdesign ,本作品轉載自: http://openclipart.org/detail/4893/effect-letters-alphabet-silver-by-chrisdesign-4893 , http://openclipart.org/detail/4893/effect-letters-alphabet-silver-by-chrisdesign-4893 http://openclipart.org/detail/4903/effect-letters-alphabet-silver-by-chrisdesign-4903 http://openclipart.org/detail/4903/effect-letters-alphabet-silver-by-chrisdesign-4903 , http://openclipart.org/detail/4911/effect-letters-alphabet-silver-by-chrisdesign-4911 http://openclipart.org/detail/4911/effect-letters-alphabet-silver-by-chrisdesign-4911 , http://openclipart.org/detail/4896/effect-letters-alphabet-silver-by-chrisdesign-4896 http://openclipart.org/detail/4896/effect-letters-alphabet-silver-by-chrisdesign-4896 , 瀏覽日期: 2013/1/21 。


Download ppt "計算機程式 第七單元 Pointers and Strings 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program,"

Similar presentations


Ads by Google