You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何理解数组变量名指向数组首地址?多维数组指针行为困惑解析

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!

1. 理解“数组变量名指向数组首个元素地址”的概念

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!

2. 多维数组的地址困惑

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. Since matrix converts to int (*)[4], adding 1 moves the pointer forward by the size of one 4-element int array (4*4=16 bytes). That's why matrix+1 is 16 bytes higher than matrix, giving you 0x7ffd9ba44d20—this is the starting address of the second subarray (matrix[1]).

  • *(matrix+1): This dereferences the int (*)[4] pointer, which gives you the second subarray itself (type int [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 as matrix+1—they're pointing to the same memory location, just with different types:

    • matrix+1 is a pointer to a 4-element int array (int (*)[4])
    • *(matrix+1) converts to a pointer to a single int (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:44:15