由於array可以自動轉成pointer,所以很多人誤以為array就是pointer,如以下程式...
1
#include
<
iostream
>
2![None.gif](/Images/OutliningIndicators/None.gif)
3
using
namespace
std;
4![None.gif](/Images/OutliningIndicators/None.gif)
5![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
int
main()
{
6![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int ia[] =
{0, 1, 2};
7
int *p = ia;
8
9
int *eof_ia = ia + sizeof(ia) / sizeof(int);
10
11![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(p < eof_ia)
{
12
cout << *p++ << endl;
13
}
14
}
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
6
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
12
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
第七行,int *p = ia;若以數學角度,p和ia是相等的,而p是pointer,ia是array,所以推得pointer就是array,但C/C++並非如此,這個=是assignment的意思,也就是將array ia assign給pointer p,經過自動轉型後,將array ia第一個element的address assign給pointer p,這也是為什麼Pascal語系的assignment使用:=而非=,就是為了要跟數學區別,以免誤解。
再做一個實驗,假如array就是pointer,理論上以下的程式將可compile成功並且執行。
1
#include
<
iostream
>
2![None.gif](/Images/OutliningIndicators/None.gif)
3
using
namespace
std;
4![None.gif](/Images/OutliningIndicators/None.gif)
5![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
int
main()
{
6![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int ia[] =
{0, 1, 2};
7
8
int *eof_ia = ia + sizeof(ia) / sizeof(int);
9
10![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(ia < eof_ia)
{
11
cout << *ia++ << endl;
12
}
13
}
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
6
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
11
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
12
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
compiler在11行不能過,可見pointer的取值方式並不適用於array,必須改成array專屬的subscripting方式才能取值。
1
#include
<
iostream
>
2![None.gif](/Images/OutliningIndicators/None.gif)
3
using
namespace
std;
4![None.gif](/Images/OutliningIndicators/None.gif)
5![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
int
main()
{
6![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int ia[] =
{0, 1, 2};
7
8
int ia_size = sizeof(ia) / sizeof(int);
9
10
int i = 0;
11![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(i < ia_size)
{
12
cout << ia[i++] << endl;
13
}
14
}
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
6
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
12
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Conclusion
array並非pointer,但array可以自動轉型成pointer,這也是array傳進function後變成pointer的理論基礎。
See Also
(原創) 當array傳至function時,是pass by value還是pass by address? (C/C++) (C)