簡體   English   中英

訪問指向動態數組 C++ 的 2D 指針中的元素

[英]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;    
    }
 }

我一直在嘗試從指向主 function 中的動態數組的 2D 指針讀取元素,但我似乎無法讓它工作,我嘗試使用雙循環和( (map + i ) + j) 但無濟於事

您需要將參數聲明為具有引用類型。 例如

void create(int ** &map);

在這種情況下,在main中聲明的指針map將在 function 內更改。

否則 function 處理其局部變量(參數) mapmain中聲明的指針map將保持不變。

使用像8這樣的幻數也是一個壞主意。 如果可以將數組的大小傳遞給 function,則 function 將更加靈活和通用。 例如

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

而不是將指針傳遞給 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';
    }
}

output:


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM