2011-12-24 2 views
3

matrix,&matrix,matrix[0]および&matrix[0]+1の違いは何ですか? ndまた、そのメモリ表現。 Cにおいて2次元マトリックスのアドレス

int main(){ 
     int matrix[4][3]; 
     printf("%u\n",&matrix); 
     printf("%u\n",&matrix+1); 
     printf(" %u\n",matrix); 
     printf("%u\n",matrix[0]+1);  
     printf(" %u\n",&matrix[0]); 
     printf(" %u\n",&matrix[0]+1); 
} 

PLATFORM ---- GCCのUbuntu 10.04

+1

にあなたの文に変換します。 –

答えて

3

、多次元アレイだけ連続したメモリブロックです。あなたの場合、4 x 3配列は12要素の連続ブロックで、 'matrix'はメモリブロックの開始アドレスへのポインタです。ここで行列、行列[0]、行列[0] [0]すべてのメモリブロック

の開始アドレスを参照してくださいコンパイラはあなたがあなた自身の質問に答えている

&matrix = get the starting address of the memory block 
&matrix+1 = add '1' to matrix datatype, i.e. add 12 (total elements in matrix) * size of int (4 bytes) = 48 bytes. 

matrix[0]+1 = address of first row, second element, i.e. &matrix[0][1] 
&matrix[0] = address of first row, first element which is nothing but starting address of matrix 
&matrix[0]+1 = add one row to starting address of matrix, i.e. 3 elements in a column * size of int(4 byte) = 12 bytes. Note that this is equivalent to (&matrix[0])+1 and not &(matrix[0]+1)