&table[0][0] 和 &table 有什么区别?
What's the difference between &table[0][0] and &table?
我已经成功地尝试将二维数组传递给函数,请参见下面的代码。我不明白的是:为什么我的“方法 C”(见下面的代码)不起作用?
&table[0][0]
和 &table
有什么区别?它们都指向同一个内存地址。前者作为传递给我的函数的参数,后者没有,错误消息:
"no known conversion from 'int (*)[3][2]' to 'int *' for 1st argument void print_table(int *table, const int ROWS, const int COLUMNS)
提前致谢!亚历克斯
#include <iostream>
void print_table(int *table, const int ROWS, const int COLUMNS)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
std::cout << table[i * COLUMNS + j] << "\t";
}
std::cout << "\n";
}
}
int main()
{
const int ROWS = 3;
const int COLUMNS = 2;
int table[ROWS][COLUMNS] = {{1,2},{3,4},{5,6}};
std::cout << (int*)table << "\n"; // these 3 couts all deliver the same memory address
std::cout << &table[0][0] << "\n"; // these 3 couts all deliver the same memory address
std::cout << &table << "\n"; // these 3 couts all deliver the same memory address
print_table((int*)table, ROWS, COLUMNS); // method A: Works.
print_table(&table[0][0], ROWS, COLUMNS); // method B: Works too.
print_table(&table, ROWS, COLUMNS); // method C: Doesn't work! Why?
return 0;
}
主要区别:实际指向的是什么,以及它的类型。
表达式 &table
指向数组 table
本身,它的类型为 int(*)[3][2]
.
表达式 &table[0][0]
是指向 sub-array table[0]
中单个元素的指针,类型为 int*
.
“方法 A”之所以有效,即使它是错误的,是因为两个指针恰好指向同一位置。
如果我们绘制出你的数组,那么它将看起来像这样(添加了“指针”):
+-------------+-------------+-------------+-------------+-------------+-------------+
| table[0][0] | table[0][1] | table[1][0] | table[1][1] | table[2][0] | table[2][1] |
+-------------+-------------+-------------+-------------+-------------+-------------+
^
|
&table
|
&table[0]
|
&table[0][0]
我添加了 &table[0]
因为这就是普通 table
将 衰减 的原因。它将具有 int(*)[2]
类型。这是您作为“方法 A”传递的指针。如果不强制转换为 int*
,该方法也会失败。
一般来说:每当您需要进行 C-style 转换(例如“方法 A”)时,您应该将其视为您做错事的标志。
简而言之:只有“方法B”才是真正正确的。
我已经成功地尝试将二维数组传递给函数,请参见下面的代码。我不明白的是:为什么我的“方法 C”(见下面的代码)不起作用?
&table[0][0]
和 &table
有什么区别?它们都指向同一个内存地址。前者作为传递给我的函数的参数,后者没有,错误消息:
"no known conversion from 'int (*)[3][2]' to 'int *' for 1st argument void print_table(int *table, const int ROWS, const int COLUMNS)
提前致谢!亚历克斯
#include <iostream>
void print_table(int *table, const int ROWS, const int COLUMNS)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
std::cout << table[i * COLUMNS + j] << "\t";
}
std::cout << "\n";
}
}
int main()
{
const int ROWS = 3;
const int COLUMNS = 2;
int table[ROWS][COLUMNS] = {{1,2},{3,4},{5,6}};
std::cout << (int*)table << "\n"; // these 3 couts all deliver the same memory address
std::cout << &table[0][0] << "\n"; // these 3 couts all deliver the same memory address
std::cout << &table << "\n"; // these 3 couts all deliver the same memory address
print_table((int*)table, ROWS, COLUMNS); // method A: Works.
print_table(&table[0][0], ROWS, COLUMNS); // method B: Works too.
print_table(&table, ROWS, COLUMNS); // method C: Doesn't work! Why?
return 0;
}
主要区别:实际指向的是什么,以及它的类型。
表达式 &table
指向数组 table
本身,它的类型为 int(*)[3][2]
.
表达式 &table[0][0]
是指向 sub-array table[0]
中单个元素的指针,类型为 int*
.
“方法 A”之所以有效,即使它是错误的,是因为两个指针恰好指向同一位置。
如果我们绘制出你的数组,那么它将看起来像这样(添加了“指针”):
+-------------+-------------+-------------+-------------+-------------+-------------+ | table[0][0] | table[0][1] | table[1][0] | table[1][1] | table[2][0] | table[2][1] | +-------------+-------------+-------------+-------------+-------------+-------------+ ^ | &table | &table[0] | &table[0][0]
我添加了 &table[0]
因为这就是普通 table
将 衰减 的原因。它将具有 int(*)[2]
类型。这是您作为“方法 A”传递的指针。如果不强制转换为 int*
,该方法也会失败。
一般来说:每当您需要进行 C-style 转换(例如“方法 A”)时,您应该将其视为您做错事的标志。
简而言之:只有“方法B”才是真正正确的。