C二维数组:第一个'level'是指针数组吗?
C two dimensional arrays: Is the first 'level' an array of pointers?
在 C 中,我们有二维数组,即 a[m][n]
。
在一维数组中 a
是指向数组开头的指针。
二维数组呢? a[i]
是否包含指向数组中 i
行开头的指针?因此 a[i]
是一个指针数组,在下面的事情 function(int **a, m, n)
?
中传递给函数
Does a[i] hold a pointer to the start of the i row in an array?
没有。 C 中二维数组的数据是一个连续的元素块加上一些巧妙的索引访问。但是二维数组是数组的数组,而不是指针的数组。
形式上,a[i]
包含一维数组。在某些上下文中,这可能会衰减为指向第 i 行的第一个元素的指针,但对于您未指定的某些类型 T
,其类型仍然是 T[n]
。
In one dimensional arrays a is a pointer to the start of the array.
不正确。 a
是一个 数组 。当您在表达式中使用 a
时,它会将 "decays" 转换为指向第一个元素的指针。为了更好地理解这一点,read this chapter of the C FAQ, particularly this one.
What about two dimensional arrays? Does a[i] hold a pointer to the start of the i row in an array?
没有。在二维数组中,a[i]
是一个数组,而 int a[x][y];
是数组的数组。任何地方都没有指针。
您可能会感到困惑,因为 C 允许这种语法:int a[][N] = ...;
,但该语法仅仅意味着数组的数组大小取决于初始化列表中的项目数。
在 C 中,我们有二维数组,即 a[m][n]
。
在一维数组中 a
是指向数组开头的指针。
二维数组呢? a[i]
是否包含指向数组中 i
行开头的指针?因此 a[i]
是一个指针数组,在下面的事情 function(int **a, m, n)
?
Does a[i] hold a pointer to the start of the i row in an array?
没有。 C 中二维数组的数据是一个连续的元素块加上一些巧妙的索引访问。但是二维数组是数组的数组,而不是指针的数组。
形式上,a[i]
包含一维数组。在某些上下文中,这可能会衰减为指向第 i 行的第一个元素的指针,但对于您未指定的某些类型 T
,其类型仍然是 T[n]
。
In one dimensional arrays a is a pointer to the start of the array.
不正确。 a
是一个 数组 。当您在表达式中使用 a
时,它会将 "decays" 转换为指向第一个元素的指针。为了更好地理解这一点,read this chapter of the C FAQ, particularly this one.
What about two dimensional arrays? Does a[i] hold a pointer to the start of the i row in an array?
没有。在二维数组中,a[i]
是一个数组,而 int a[x][y];
是数组的数组。任何地方都没有指针。
您可能会感到困惑,因为 C 允许这种语法:int a[][N] = ...;
,但该语法仅仅意味着数组的数组大小取决于初始化列表中的项目数。