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

ε祈祈猫儿з 提交于 2021-02-10 15:47:45

问题


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?

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)

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;
}

回答1:


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].

The expression &table[0][0] is a pointer to a single element in the sub-array table[0], and will have the type int*.

The reason "method A" works, even though it's wrong, is because both pointers just happen to be pointing to the same location.

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. 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.

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.

In short: Only "method B" is really correct.



来源:https://stackoverflow.com/questions/64617513/whats-the-difference-between-table00-and-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!