C++多维数组逗号索引地址
C++ Multi-dimensional Array Comma Index Address
在玩多维数组时,我发现如果我使用逗号分隔索引,它会 return 给我数组中元素的地址,忽略第一个逗号。以下示例显示了这一点:
int arr[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Output arr[,] test
cout << "arr[" << i << "," << j << "]: " << arr[i,j] << endl;
}
}
cout << "\n--------------\n" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Output arr[][] test
cout << "arr[" << i << "][" << j << "]: " << &arr[i][j] << endl;
}
}
这给出了输出:
arr[0,0]: 0x28feec
arr[0,1]: 0x28fef8
arr[0,2]: 0x28ff04
arr[1,0]: 0x28feec
arr[1,1]: 0x28fef8
arr[1,2]: 0x28ff04
arr[2,0]: 0x28feec
arr[2,1]: 0x28fef8
arr[2,2]: 0x28ff04
--------------
arr[0][0]: 0x28feec
arr[0][1]: 0x28fef0
arr[0][2]: 0x28fef4
arr[1][0]: 0x28fef8
arr[1][1]: 0x28fefc
arr[1][2]: 0x28ff00
arr[2][0]: 0x28ff04
arr[2][1]: 0x28ff08
arr[2][2]: 0x28ff0c
如您所见,[0,0]
/[0][0]
、[0,1]
/[1][0]
和 [0,2]
/[2][0]
处的地址值配对。它们也匹配 [n,n]
对中的第二个与 [n][n]
对中的第一个数字相匹配的地方。
我发现这个Wikipedia article关于C++中的逗号运算符同意这个跳过:
In Pascal, multidimensional arrays are indexed using commas, e.g. A[i, j]
. In C, however, A[i, j]
is equivalent to A[j]
, since the value of i
is discarded. The correct way to index multidimensional arrays in C is with a construction like A[i][j]
.
我唯一的问题是:为什么它是 return 地址? 我试图找到这背后的原因,但完全被难住了。维基百科文章似乎不同意它应该 return 一个地址,因为它说它是 "equivalent to A[j]
"。我将非常感谢对此的回答。提前致谢!
当您处理多维数组时,索引由逗号 arr[i,j]
分隔,逗号的行为类似于 comma operator。
逗号运算符是一个二元运算符,它计算所有以逗号分隔的表达式以完成其副作用并丢弃它直到列表中的最后一个表达式。如此有效,它忽略了除你的特定情况下的最后一个索引之外的所有索引,即 j
arr[j]
。
由于arr[j]
的类型无非是int arr[3],它衰减为一个指针int *
,所以打印指针的值显示地址。
To expand on this, ostream
operator
does not have an overload which can accept a type Ty[]
, the closest
it accepts is Ty*
,
ostream& operator<< (void* val);
so that effectively means, Ty[]
decays to Ty*
which is then downcasted to void *
and calls
ostream& operator<< (void* val)
, which prints the address of the pointer
The Wikipedia article seems to disagree that it should return an
address, because it says that it is "equivalent to A[j]". I will very
much appreciate an answer to this.
为简单起见,您希望程序在以下情况下打印什么?
int arr[3];
std::cout << arr << std::endl;
在玩多维数组时,我发现如果我使用逗号分隔索引,它会 return 给我数组中元素的地址,忽略第一个逗号。以下示例显示了这一点:
int arr[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Output arr[,] test
cout << "arr[" << i << "," << j << "]: " << arr[i,j] << endl;
}
}
cout << "\n--------------\n" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Output arr[][] test
cout << "arr[" << i << "][" << j << "]: " << &arr[i][j] << endl;
}
}
这给出了输出:
arr[0,0]: 0x28feec
arr[0,1]: 0x28fef8
arr[0,2]: 0x28ff04
arr[1,0]: 0x28feec
arr[1,1]: 0x28fef8
arr[1,2]: 0x28ff04
arr[2,0]: 0x28feec
arr[2,1]: 0x28fef8
arr[2,2]: 0x28ff04
--------------
arr[0][0]: 0x28feec
arr[0][1]: 0x28fef0
arr[0][2]: 0x28fef4
arr[1][0]: 0x28fef8
arr[1][1]: 0x28fefc
arr[1][2]: 0x28ff00
arr[2][0]: 0x28ff04
arr[2][1]: 0x28ff08
arr[2][2]: 0x28ff0c
如您所见,[0,0]
/[0][0]
、[0,1]
/[1][0]
和 [0,2]
/[2][0]
处的地址值配对。它们也匹配 [n,n]
对中的第二个与 [n][n]
对中的第一个数字相匹配的地方。
我发现这个Wikipedia article关于C++中的逗号运算符同意这个跳过:
In Pascal, multidimensional arrays are indexed using commas, e.g.
A[i, j]
. In C, however,A[i, j]
is equivalent toA[j]
, since the value ofi
is discarded. The correct way to index multidimensional arrays in C is with a construction likeA[i][j]
.
我唯一的问题是:为什么它是 return 地址? 我试图找到这背后的原因,但完全被难住了。维基百科文章似乎不同意它应该 return 一个地址,因为它说它是 "equivalent to A[j]
"。我将非常感谢对此的回答。提前致谢!
当您处理多维数组时,索引由逗号 arr[i,j]
分隔,逗号的行为类似于 comma operator。
逗号运算符是一个二元运算符,它计算所有以逗号分隔的表达式以完成其副作用并丢弃它直到列表中的最后一个表达式。如此有效,它忽略了除你的特定情况下的最后一个索引之外的所有索引,即 j
arr[j]
。
由于arr[j]
的类型无非是int arr[3],它衰减为一个指针int *
,所以打印指针的值显示地址。
To expand on this, ostream operator does not have an overload which can accept a type
Ty[]
, the closest it accepts isTy*
,ostream& operator<< (void* val);
so that effectively means,
Ty[]
decays toTy*
which is then downcasted tovoid *
and callsostream& operator<< (void* val)
, which prints the address of the pointer
The Wikipedia article seems to disagree that it should return an address, because it says that it is "equivalent to A[j]". I will very much appreciate an answer to this.
为简单起见,您希望程序在以下情况下打印什么?
int arr[3];
std::cout << arr << std::endl;