計算機程式 第七單元 pointers and strings 授課教師:廖婉君教授...
TRANSCRIPT
1
計算機程式第七單元 Pointers and Strings
授課教師:廖婉君教授
【 本 著 作 除 另 有 註 明 外 , 採 取 創用 CC
「姓名標示-非商業性-相同方式分享」台灣 3.0
版授權釋出】本課程指定教材為 C++ How to Program, 7/e, Harvey M. Deitel and Paul J. Deitel, both from Deitel & Associates, Inc. ©
2010 。 本講義僅引用部分內容,請讀者自行準備。本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據Microsoft服務合約及著作權法第 46 、 52 、 65 條合理使用。
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???
Note: A pointer can only be assigned to 0, NULL, and an address.
12
aptr a
3
* and & Operators Operators Associativity Type() [] left to right highest++ -- static_cast< type >( operand )
left to right unary (postfix)
++ -- + - ! & * right to left unary (prefix)* / % left to right multiplicative+ - left to right additive<< >> left to right insertion/
extraction< <= > >= left to right relational== != left to right equality&& left to right logical AND|| left to right logical OR?: right to left conditional= += -= *= /= %= right to left assignment, left to right comma
4
Call-by-Reference with Pointer • Fig. 8.7 (p. 352) vs. Fig. 6.18 (p.226)
5
p.351
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
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[]
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]
10
p.359-361
11
Note: sizeof
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();
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
14
Function Pointer (cont.)
p.374-376
版權聲明
15
頁碼 作品 版權圖示 來源 / 作者1-15
本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法第 46 、 52 、 65 條合理使用。
2C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel ,出版社: Deitel & Associates ,出版日期: 2010 , P.348 。依據著作權法第 46 、 52 、 65 條合理使用。
3C++ 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 。
12
Open Clip Art Library ,作者: Chrisdesign ,本作品轉載自: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/4911/effect-letters-alphabet-silver-by-chrisdesign-4911,http://openclipart.org/detail/4896/effect-letters-alphabet-silver-by-chrisdesign-4896,瀏覽日期: 2013/1/21 。