2011-12-24 44 views
3

我想知道matrix,&matrix,matrix[0]&matrix[0]+1之間的區別是什麼? nd還有它的內存表示。2D Matrix中的地址

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

你已經回答了你自己的問題。 – 2011-12-24 10:20:26

回答

3

在C中,多維陣列僅僅是一個連續的存儲器塊。在你的情況下,一個4×3的數組是12個元素的連續塊,'矩陣'是指向內存塊的起始地址的指針。這裏矩陣,矩陣[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)