Why can I access an array of pointers with two parameters, when it's defined as one-dimensional?
为什么我可以访问一个有两个参数的指针数组,当它被定义为一维的时候?
I know, I have to work with an array of pointers to access a multi-dimensional array in a function, but I don't know why I can access the array of pointers with two parameters.
我知道,我必须使用指针数组来访问函数中的多维数组,但是我不知道为什么我可以用两个参数来访问指针数组。
int a[m][l] { 1,2,3,4, 2,3,4,5, 3,4,5,6 }; //some matrices
int c[m][l];
int *ap[m]; //array of pointers one-dimensional
int i,j;
for (i = 0; i < m; i++) //passing the address of first element in each
ap[i] = a[i]; //colon of matrix to array of pointers
for (j = 0; j < m; j++)
bp[i] = b[i];
dosomethingwithmatrix(ap[0], bp[0], m, l);
int* dosomethingwithmatrix(const int*ap[], int* bp[])
{
cp[i][j] = ap[i][j] //accss the array of pointers with two parameters
}
4 个解决方案
#1
1
In your case, ap[i][j]
is allowed because, it is meaning ful.
在你的例子中,ap[i][j]是允许的,因为它是有意义的。
Let's check the data types.
让我们检查一下数据类型。
-
For
int *ap[m];
,ap
is an array ofint *
s. In case of the function parameterint*ap[]
,ap
is a pointer to a pointer-to-int.对于int *ap[m];, ap是一个int *s数组。对于函数参数int*ap[], ap是指向指针对int的指针。
-
Then,
ap[k]
(referring to the previous point) is anint *
. This may very well be allocated memory which can provide valid access to more that oneint
s.然后,ap[k](参照前一点)是一个int *。这很可能是分配的内存,它可以提供对更多ints的有效访问。
-
Provided enough memory allocated,
ap[k][s]
will refer to anint
.如果分配了足够的内存,ap[k][s]将引用一个int类型。
#2
2
Also in C an array is said to decay into a pointer.
同样在C数组中,数组被称为衰变为指针。
From the standard (C99 6.3.2.1/3 - Other operands - Lvalues, arrays, and function designators):
从标准(c996.3.2.1 /3 -其他操作数- Lvalues、数组和函数指示器):
Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.
除非sizeof运算符的操作数或一元&操作符,或者是一个字符串文字用来初始化一个数组,一个表达式的类型数组类型的”转化为一种表达“指针式”型指向数组的初始元素对象并不是一个左值。
So:
所以:
array[i] "decays" to pointer[i]
where pointer has the address of the [0]th element of array
And since we have already seen that:
因为我们已经看到了
p[i] == *(p + i)
all we are doing is adding offsets to pointers.
我们所做的只是为指针添加偏移量。
BTW, since addition is commutative, *(p + i) == *(i + p)
, it gives the sometimes surprising result that:
顺便说一下,由于加法是可交换的,*(p + i) = *(i + p),所以有时会产生令人惊讶的结果:
3["hello world"]
is a perfectly valid C expression (and it equals "hello world"[3]
).
是一个完全有效的C表达式(它等于“hello world”[3])。
#3
1
Because you can dereference the pointer with index notation. First you access the element (the pointer) with an index, now the pointer can be dereferenced too with an index.
因为你可以用索引表示法去引用指针。首先使用索引访问元素(指针),现在也可以使用索引取消引用。
The equivalency between that and the indirection operator is as follows
它与间接运算符之间的等价性如下
pointer[i] == *(pointer + i);
#4
0
Both of the parameters ap
and bp
of function dosomethingwithmatrix
are pointer to pointer to int. They are not array of pointers. The function declarator
函数dosomethingwithmatrix的参数ap和bp都是指向int的指针,它们不是指针的数组。函数说明符
int* dosomethingwithmatrix(const int *ap[], int *bp[])
is equivalent to
相当于
int* dosomethingwithmatrix(const int **ap, int **bp)
#1
1
In your case, ap[i][j]
is allowed because, it is meaning ful.
在你的例子中,ap[i][j]是允许的,因为它是有意义的。
Let's check the data types.
让我们检查一下数据类型。
-
For
int *ap[m];
,ap
is an array ofint *
s. In case of the function parameterint*ap[]
,ap
is a pointer to a pointer-to-int.对于int *ap[m];, ap是一个int *s数组。对于函数参数int*ap[], ap是指向指针对int的指针。
-
Then,
ap[k]
(referring to the previous point) is anint *
. This may very well be allocated memory which can provide valid access to more that oneint
s.然后,ap[k](参照前一点)是一个int *。这很可能是分配的内存,它可以提供对更多ints的有效访问。
-
Provided enough memory allocated,
ap[k][s]
will refer to anint
.如果分配了足够的内存,ap[k][s]将引用一个int类型。
#2
2
Also in C an array is said to decay into a pointer.
同样在C数组中,数组被称为衰变为指针。
From the standard (C99 6.3.2.1/3 - Other operands - Lvalues, arrays, and function designators):
从标准(c996.3.2.1 /3 -其他操作数- Lvalues、数组和函数指示器):
Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.
除非sizeof运算符的操作数或一元&操作符,或者是一个字符串文字用来初始化一个数组,一个表达式的类型数组类型的”转化为一种表达“指针式”型指向数组的初始元素对象并不是一个左值。
So:
所以:
array[i] "decays" to pointer[i]
where pointer has the address of the [0]th element of array
And since we have already seen that:
因为我们已经看到了
p[i] == *(p + i)
all we are doing is adding offsets to pointers.
我们所做的只是为指针添加偏移量。
BTW, since addition is commutative, *(p + i) == *(i + p)
, it gives the sometimes surprising result that:
顺便说一下,由于加法是可交换的,*(p + i) = *(i + p),所以有时会产生令人惊讶的结果:
3["hello world"]
is a perfectly valid C expression (and it equals "hello world"[3]
).
是一个完全有效的C表达式(它等于“hello world”[3])。
#3
1
Because you can dereference the pointer with index notation. First you access the element (the pointer) with an index, now the pointer can be dereferenced too with an index.
因为你可以用索引表示法去引用指针。首先使用索引访问元素(指针),现在也可以使用索引取消引用。
The equivalency between that and the indirection operator is as follows
它与间接运算符之间的等价性如下
pointer[i] == *(pointer + i);
#4
0
Both of the parameters ap
and bp
of function dosomethingwithmatrix
are pointer to pointer to int. They are not array of pointers. The function declarator
函数dosomethingwithmatrix的参数ap和bp都是指向int的指针,它们不是指针的数组。函数说明符
int* dosomethingwithmatrix(const int *ap[], int *bp[])
is equivalent to
相当于
int* dosomethingwithmatrix(const int **ap, int **bp)