简体   繁体   English

&table[0][0] 和 &table 有什么区别?

[英]What's the difference between &table[0][0] and &table?

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?我不明白的是:为什么我的“方法 C”(见下面的代码)不起作用?

What's the difference between &table[0][0] and &table ? &table[0][0]&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)

Thanks in advance!提前致谢! Alex亚历克斯

#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.主要区别:实际指向的是什么,以及它的类型。

The expression &table points to the array table itself, and it will have the type int(*)[3][2] .表达式&table指向数组table本身,它的类型为int(*)[3][2]

The expression &table[0][0] is a pointer to a single element in the sub-array table[0] , and will have the type int* .表达式&table[0][0]是指向子数组table[0]单个元素的指针,其类型为int*

The reason "method A" works, even though it's wrong, is because both pointers just happen to be pointing to the same location. “方法 A”有效的原因,即使它是错误的,也是因为两个指针恰好指向同一个位置。

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]

I added &table[0] because this is what plain table will be decaying to.我添加了&table[0]因为这是普通table衰减的。 It will have the type int(*)[2] .它将具有int(*)[2] This is the pointer you pass as "method A".这是您作为“方法 A”传递的指针。 Without the casting to int* that method would also fail.如果不转换为int* ,该方法也会失败。

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.一般而言:每当您需要进行 C 样式转换(例如“方法 A”)时,您都应该将其视为您做错了什么的标志。

In short: Only "method B" is really correct.简而言之:只有“方法B”才是真正正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM