& table [0] [0]和& table有什么区别? [英] What's the difference between &table[0][0] and &table?
问题描述
我已经成功尝试将2D数组传递给函数,请参见下面的代码.我没有得到什么:为什么我的方法C"会被删除?(请参见下面的代码)不起作用?
I've been successfully trying to pass a 2D-array to a function, see code below. What I don't get: Why does my "method C" (see code below) not work?
& table [0] [0]
和& table
之间有什么区别?他们两个都指向相同的内存地址.前者作为传递给我函数的参数,后者则不,错误消息:
What's the difference between &table[0][0]
and &table
? Both of them point to the same memory address. The former works as argument passed to my function, the latter doesn't, error message:
"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;
}
推荐答案
主要区别:实际指向的内容及其类型.
Main difference: What is actually being pointed to, and from that its type.
表达式& table
指向数组 table
本身,其类型为 int(*)[3] [2] 代码>.
The expression &table
points to the array table
itself, and it will have the type int(*)[3][2]
.
表达式& table [0] [0]
是指向子数组 table [0]
中单个元素的指针,其类型为 int *
.
The expression &table[0][0]
is a pointer to a single element in the sub-array table[0]
, and will have the type int*
.
原因方法A"是即使错了,仍然有效,是因为两个指针恰好都指向相同的位置.
The reason "method A" works, even though it's wrong, is because both pointers just happen to be pointing to the same location.
如果我们绘制您的数组,那么它将看起来像这样(添加了"pointers"):
If we draw out your array then it will look something like this (with "pointers" added):
+-------------+-------------+-------------+-------------+-------------+-------------+
| 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 *
,该方法也将失败.
I added &table[0]
because this is what plain table
will be decaying to. It will have the type int(*)[2]
. This is the pointer you pass as "method A". Without the casting to int*
that method would also fail.
通常来说:每当您需要进行C样式转换(例如方法A")时,都应将其视为做错了事的迹象.
Generally speaking: Whenever you need to do a C-style casting (like for "method A") then you should take that as a sign that you're doing something wrong.
简而言之:仅方法B"是真的.
In short: Only "method B" is really correct.
这篇关于& table [0] [0]和& table有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!