如何理解数组变量名指向数组首地址?多维数组指针行为困惑解析
Hey there, let's break down your two C array questions one by one—they're super common confusions when you're digging into how arrays work under the hood!
First off, your intuition is spot-on: the array name array isn't a separate pointer variable with its own allocated memory. It's essentially a compiler-friendly label that refers to the entire array, but in almost all expressions, it undergoes an implicit array-to-pointer conversion—meaning it automatically gets treated as a pointer to the array's first element.
Let's look at your code example:
void main() { int array[10] = {1,2,3,4,5,6,7}; printf("%p\n",array); }
When you run this, the stack allocates 10 int slots (40 bytes, assuming a 4-byte int). The name array doesn't take up any extra space here—it's just a symbolic reference the compiler uses to know where the array starts.
So why does printf accept it as a pointer? Because when you pass array to printf, that implicit conversion kicks in: array gets converted to a int* pointer pointing to the first element (array[0]). The %p format specifier expects a pointer value, so this conversion makes it work perfectly.
A quick way to confirm the difference between an array name and a real pointer variable: try sizeof(array)—it'll return 40 (the full size of the array), whereas if you assigned int* ptr = array; and did sizeof(ptr), you'd get 4 or 8 bytes (the size of a pointer on your system). That's one of the few cases where the array name doesn't convert to a pointer!
Let's unpack this with your code and the output you saw:
int main() { int matrix[2][4] = {{11,22,33,99},{44,55,66,110}}; printf("%p\n", matrix); printf("%p\n", matrix+1); printf("%p\n", *(matrix+1)); }
Output: 0x7ffd9ba44d10 0x7ffd9ba44d20 0x7ffd9ba44d20
Here's the key: matrix is a 2-element array where each element is itself a 4-element int array. Its type is int [2][4]. When matrix is used in an expression (again, except for sizeof or &), it converts to a pointer to its first element—so that's a pointer to a 4-element int array, written as int (*)[4].
matrix+1: Pointer arithmetic works based on the type the pointer points to. Sincematrixconverts toint (*)[4], adding 1 moves the pointer forward by the size of one 4-elementintarray (4*4=16 bytes). That's whymatrix+1is 16 bytes higher thanmatrix, giving you0x7ffd9ba44d20—this is the starting address of the second subarray (matrix[1]).*(matrix+1): This dereferences theint (*)[4]pointer, which gives you the second subarray itself (typeint [4]). But wait—this subarray name then undergoes the same array-to-pointer conversion we talked about earlier! It gets converted to a pointer to its first element (matrix[1][0]), which is exactly the starting address of the second subarray. That's why the address value is the same asmatrix+1—they're pointing to the same memory location, just with different types:matrix+1is a pointer to a 4-elementintarray (int (*)[4])*(matrix+1)converts to a pointer to a singleint(int*)
To test this type difference, try printing *(*(matrix+1))—you'll get 44, the value of matrix[1][0]. And sizeof(*(matrix+1)) will return 16 (the size of the 4-element subarray), whereas sizeof(matrix+1) will return 4 or 8 (the size of a pointer).
内容的提问来源于stack exchange,提问作者Darshan L

