2D矩阵中的地址

时间:2011-12-24 10:12:30

标签: c matrix memory-address

我想知道matrix&matrixmatrix[0]&matrix[0]+1之间有什么区别? nd也是它的记忆表示。

 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 个答案:

答案 0 :(得分:3)

在C中,多维数组只是一个连续的内存块。在您的情况下,4 x 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)
相关问题