使用指针表示法在C中访问多维数组 [英] Accessing multi-dimensional arrays in C using pointer notation
问题描述
让 a 是一个由10个整数组成的一维数组,如下所示
Let a be an one dimensional array of 10 integers as shown below
int a[10];
以及上面声明的数组的表达式
and for above declared array the expression
a[i]
评估为 *(a + i),并给出存储在 i th 索引中的值,例如
is evaluated as *(a+i) and gives the value stored at the ith index,e.g.
int a[5]={1,2,3,4,5};
此处 a [3] 的值为 *(a + 3),等于 4 ,即存储在数组 a 的 3rd 索引处的元素的值. 但是在这种二维数组的情况下
here a[3] is evaluated as *(a+3) which is equal to 4 ,i.e. the value of element stored at 3rd index of array a. But in case of a 2D array like this one
int b[2][3]={ {1,2,3},{4,5,6} };
(这里我将2D数组b视为1D数组的数组,即b是具有2个元素的1D数组,每个元素本身都是3个int的1D数组) 如果我们使用单个下标运算符,数组名称为
(here I am thinking of 2D array b as array of 1D arrays,i.e. b is a 1D array having 2 elements,each element itself being an 1D array of 3 int) if we use single subscript operator with array name as
b[i]
i = 1的上述表达式给出了存储元素4的地址,所以
the above expression for i=1 gives address where the element 4 is stored,so
- 当b是2D数组时,如何评估表达式 b [i] ?
- 当b是2D数组时,如何评估表达式 b [i] [j] ?
- 如何使用指针访问C中多维数组的元素.在这里,我想了解编译器如何在内部处理多维数组?
- 如果 b 是2D数组, b 会如何评估?
- How is the expression b[i] evaluated in case when b is 2D array?
- How is the expression b[i][j] evaluated in case when b is 2D array?
- How to access elements of a Multi-dimensional Array in C using pointers. Here I would like to know about how compiler treats a Multi-dimensional Array internally ?
- What does b evaluate to if b is a 2D array?
推荐答案
在这里,我将2D数组b视为1D数组
here i am thinking of 2D array b as array of 1D arrays
这是解决此问题的正确方法,因为确实如此. C标准实际上并未将多维数组指定为某些特殊情况,但是由于数组的类型与其他数组相同,因此它们是可能的.这样我们就可以拥有一个数组数组.
That's the correct way to approach this, because that's indeed what it is. The C standard does not actually specify multi-dimensional arrays as some special case, but they are rather possible because an array is a type like any other. So we can have an array of arrays.
- 当b是2D数组时,如何计算b [i]表达式?
...
- How is the expression b[i] evaluated in case when b is 2D array?
...
- 如果b是2D数组,b会得出什么结果?
对于任何数组,b
在表达式中使用时,都会衰减为指向第一个元素的指针.因此,b[i]
与*(b+i)
等效,对于任何其他数组表达式.
As for any array, b
when used in an expression, decays into a pointer to the first element. b[i]
is therefore equivalent to *(b+i)
, as for any other array expression.
在这种情况下,b
会衰减为类型为int(*)[3]
的数组指针.
In this case b
decays into an array pointer of type int(*)[3]
.
- 当b是2D数组时,表达式b [i] [j]的值如何计算?
-
b
在表达式中使用,因此在该表达式中它衰减为指向第一个元素的指针,该元素是指向第一个数组的数组指针. -
b[i]
导致将指针算术应用于数组指针,等效于*(b+i)
.这给出了数组编号i
. - 在此位置,我们有一个类型为
int[3]
的一维数组.由于此数组是另一个数组的一部分,因此它本身没有标识符.但是为了说明起见,让我们假设它得到一个临时名称"tmp
".然后,我们将得到表达式tmp[j]
,它一如既往地衰减为*(tmp+j)
,从而导致int
. b
is used in an expression, so in this expression it decays into a pointer to the first element, which is an array pointer to the first array.b[i]
causes pointer arithmetic to get applied to the array pointer, equivalent to*(b+i)
. This gives array numberi
.- At this position we have a 1D array of type
int[3]
. Since this array is part of another array, it has no identifier by itself. But for the sake of illustration, lets pretend it gets a temporary name "tmp
". We would then have the expressiontmp[j]
, which as always decays into*(tmp+j)
, resulting in anint
. - 如何在C语言中使用指针访问多维数组的元素.这里我想了解编译器如何在内部处理多维数组?
基本上整个表达式都可以视为*(*(b+i) + j)
.
Essentially the whole expression can be treated as *(*(b+i) + j)
.
如上所述,它将其视为数组数组.例如,您可以使用数组指针遍历2D数组:
As explained above, it treats it as an array of arrays. For example, you can iterate over a 2D array by using array pointers:
#include <stdio.h>
void print_array (int array[3])
{
printf("%d %d %d\n", array[0], array[1], array[2]);
}
int main (void)
{
int b[2][3]={ {1,2,3},{4,5,6} };
const size_t b_size = sizeof b / sizeof *b;
for(int(*ptr)[3] = b; ptr < b+b_size; ptr++)
{
print_array(*ptr);
}
}
这篇关于使用指针表示法在C中访问多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!