2012-11-02 75 views
-4

這是一個非常基本的概念。但是,我一般都感到困惑。幫我。數組元素值,元素地址和指針增量

情況1:

對於下面的代碼:

int oneDArray[] = {1,2,3}; 
cout<<&oneDArray<<endl; 
cout<<oneDArray<<endl; 
cout<<&oneDArray+1<<endl; 
cout<<oneDArray+1<<endl; 

輸出是:

0x28fef4 
0x28fef4 
0x28ff00 
0x28fef8 

爲什麼會出現在遞增的值的差異?

案例2:

int arr[2][3] = {{1,2,3}, {4,5,6}}; 
    cout<<&arr<<endl; 
    cout<<arr<<endl; 
    cout<<*arr<<endl; 

    cout<<&arr+1<<endl; 
    cout<<arr+1<<endl; 
    cout<<*arr+1<<endl; 

輸出是:

0x28fee8 
0x28fee8 
0x28fee8 
0x28ff00 
0x28fef4 
0x28feec 

爲什麼輸出相同的ARR & *改編? (它如何在內部工作) 爲什麼遞增值有差異?

+0

你還希望從中得到什麼?數組的名稱已經返回第一個元素的地址。添加名稱試圖使它成爲一個指針並返回地址,但地址仍然相同,但我會說奇怪。你爲什麼做這個? – fayyazkl

+1

非常糟糕的問題 – jondinham

+1

我會對另一個「爲什麼」感興趣:**爲什麼你沒有使用Google或搜索,**或者**爲什麼你沒有仔細閱讀C教程?** – 2012-11-02 06:26:19

回答

1

爲了您的一維數組:

//starting at memory address: 0x28FEF4 
int My_Array[3] = {1,2,3}; 

//memory dumping, each 'int' element takes 4 bytes, 
//high bytes are at higher addresses 
0x28FEF4 01 00 00 00 
0x28FEF8 02 00 00 00 
0x28FEFC 03 00 00 00 

這是C語言翻譯爲英語語言:

My_Array  = (address) of-the (first-byte) of (My_Array) 
My_Array[0] = (value) of-the (element-at-index-0) 
My_Array[k] = (value) of-the (element-at-index-k) 

&My_Array = (address) of-the (first-byte) of (My_Array) 
&My_Array[0] = (address) of-the (first-byte) of (element-at-index-0) 
&My_Array[k] = (address) of-the (first-byte) of (element-at-index-k) 

*My_Array = (value) pointed-by (My_Array) 
*My_Array[0] = (value) pointed-by (My_Array[0]) 
*My_Array[k] = (value) pointed-by (My_Array[k]) 

The values of 'My_Array' and '&My_Array' are always identical for array type 
but there's a difference when using them with mathematical operators. 
My_Array is a pointer to 'int' type, while &My_Array is a pointer 
to 'int[3]' type. When incremented, the values added in are sizeof(int) 
and sizeof(int[3]) respectively. 

The last two notations: *My_Array[0] and *My_Array[k] 
are valid in mathematical sense but not valid in C language when using 
with single-dimension array because C langugae doesn't allow using 
'int' value as pointer. 

解釋你的情況1的輸出結果:

(&My_Array) is identical to (My_Array) = 
0x28FEF4 

(My_Array) is identical to (&My_Array) = 
0x28FEF4 

(&My_Array+1) = 0x28FEF4 + sizeof(int[3]) = 0x28FEF4 + 12 = 
0x28FF00 

(My_Array+1) = 0x28FEF4 + sizeof(int) = 0x28FEF4 + 4 = 
0x28FEF8 

請將上面的註釋應用於您的第二種情況,注意以下幾點:

(1) Value of element in single dimension array is the base type. Eg: 
    int My_Array[3]; //base type is 'int' 

(2) Value of element in multiple dimension array is either pointer 
    or base type. Eg: 
    int My_Array[2][3][4]; //base type is 'int' 

    My_Array is a pointer to 'int' type   
    My_Array[i] is a pointer to 'int' type 
    My_Array[i][j] is a pointer to 'int' type 
    My_Array[i][j][k] is an 'int' value 

    &My_Array is a pointer to 'int[2][3][4]' 
    &My_Array[i] is a pointer to 'int[3][4]' 
    &My_Array[i][j] is a pointer to 'int[4]' 
    &My_Array[i][j][k] is a pointer to 'int' 

(3) Values of 'My_Array' and '&My_Array' are always equal, 
    however, when dealing with operators, they work differently. 
    Eg. 
    int My_Array[2][3][4]; //base type is 'int' 

    '&My_Array' here is a pointer to 'int[2][3][4]', but 
    'My_Array' is a pointer to base type 'int'. 

(4) A pointer is always incremented by the size of the type 
    that it points to. 'int' has size of 4 bytes. 
    Eg. 
    int My_Array[2];   //type size = (2)*4 
    int My_Array[2][3];  //type size = (2*3)*4 
    int My_Array[2][3][4]; //type size = (2*3*4)*4 
    int My_Array[2][3][4][5]; //type size = (2*3*4*5)*4 
+1

這是一個很好的解釋。謝謝! –

+1

有一次,我問A教授一個非常愚蠢的問題,他只是對我喊道:「你什麼都沒學,只是回去找你的筆記。同樣的問題,我從另一位教授B那裏問到,他說:「我有點忙,午飯後見面,我會解釋你的。」而且,他確實解釋了我。差異不在於他們的知識,而在於他們的態度。一個是Assit。其他的教授是導演。 –