访问指向动态数组 C++ 的二维指针中的元素

accessing elements in 2D pointer pointing to an dynamic array C++

#include <iostream>

using namespace std;

void create(int** map);

int main() {
    int** map;
    create(map);
    cout << endl << "printing map in main" << endl;
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            map[i][j] = 1;
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

void create(int** map) {
    int x = 8, y = 8;

    map = new int* [x];

    for (int i = 0; i < x; i++) {
        map[i] = new int[y];
    }
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            map[i][j] = 1;
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
}

我一直在尝试从 2D 指针中读取元素,该指针指向 main 函数中的动态数组,但我似乎无法让它工作,我尝试使用双 for 循环读取它并且((map + i) + j) 但无济于事

您需要将参数声明为具有引用类型。例如

void create(int ** &map);

在这种情况下,main 中声明的指针 map 将在函数内更改。

否则函数处理它的局部变量(参数)mapmain中声明的指针map将保持不变..

另外,使用像 8 这样的幻数也是个坏主意。如果可以将数组的大小传递给函数,函数将更加灵活和通用。例如

void create( int ** &map, size_t m, size_t n ) 
{
    map = new int * [m];

    for ( size_t i = 0; i < m; i++ ) 
    {
        map[i] = new int[n];
    }

    for ( size_t i = 0; i < m; i++ ) 
    {
        for ( size_t j = 0; j < n; j++ ) 
        {
            map[i][j] = 1;
            cout << map[i][j] << " ";
        }
        cout << endl;    
    }
}

主要你可以写

size_t m = 8, n = 8;

int **map  = nullptr;

create( map, m, n );

cout << endl << "printing map in main" << endl;

for ( size_t i = 0; i < m; i++ ) 
{
    for ( size_t j = 0; j < n; j++ ) 
    {
        cout << map[i][j] << " ";
    }
    cout << endl;
}

您应该 return 它而不是将指针传递给 create() 。像这样:

int main(){
    int **map = create();
    cout << endl << "printing map in main" << endl;
    return 0;
}

int ** create() {
    int x = 8, y = 8;

    int ** map = new int* [x];

    for (int i = 0; i < x; i++) {
        map[i] = new int[y];
    }

    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            map[i][j] = 1;
            cout << map[i][j] << " ";
        }
        cout << endl; 
    }
   
    return map;
 }

您还应该为分配的矩阵添加删除。

您的代码经过一些重构:

#include <iostream>


int** create2DArray( const std::size_t rowCount, const std::size_t colCount );
void delete2DArray( int** const array2D, const std::size_t rowCount );
void print2DArray( int** const array2D, const std::size_t rowCount, const std::size_t colCount );

int main( )
{
    constexpr std::size_t ROW_COUNT { 8 };
    constexpr std::size_t COL_COUNT { 8 };

    int** map { create2DArray( ROW_COUNT, COL_COUNT ) };

    std::cout << '\n' << "printing map in main" << '\n';
    print2DArray( map, ROW_COUNT, COL_COUNT );

    // after work is done with array, delete it
    delete2DArray( map, ROW_COUNT );
    map = nullptr; // to prevent it from becoming a dangling pointer

    return 0;
}

int** create2DArray( const std::size_t rowCount, const std::size_t colCount )
{
    int** const array2D = new int* [rowCount];

    for ( std::size_t row = 0; row < rowCount; ++row )
    {
        array2D[row] = new int[colCount];
    }

    for ( std::size_t row = 0; row < rowCount; ++row )
    {
        for ( std::size_t col = 0; col < colCount; ++col )
        {
            array2D[row][col] = 1;
        }
    }

    return array2D;
}

void delete2DArray( int** const array2D, const std::size_t rowCount )
{
    for ( std::size_t row = 0; row < rowCount; ++row )
    {
        delete[] array2D[row];
    }

    delete[] array2D;
}

void print2DArray( int** const array2D, const std::size_t rowCount, const std::size_t colCount )
{
    for ( std::size_t row = 0; row < rowCount; ++row )
    {
        for ( std::size_t col = 0; col < colCount; ++col )
        {
            std::cout << array2D[row][col] << " ";
        }

        std::cout << '\n';
    }
}

输出:


printing map in main
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1

注意 #1: 当在变量声明中使用 constexpr 时,该变量在编译时计算,而不是 运行-时间并成为一个常量表达式。因此,最好将具有常量值的变量(例如 53.7 等立即数)声明为 constexpr.
查看 constexpr (C++) 了解更多详情。

注#2: std::size_t 是无符号整数类型。在我的编译器上,它相当于 unsigned long long int(顺便说一句,它的大小为 8 个字节)。
这是来自这个 std::size_t:

typedef /*implementation-defined*/ size_t;