我想知道matrix
,&matrix
,matrix[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
答案 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)