繁体   English   中英

将指向二维数组的指针分配给数组

[英]Assign pointer to 2d array to an array

所以我得到了一个 function 它创建了我的二维数组并用测试数据填充它。 现在我需要将指针分配给数组

//Fill matrix with test data
int *testArrData(int m, int n){
    int arr[n][m];
    int* ptr;
    ptr = &arr[0][0];
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            *((ptr+i*n)+j) = rand()%10;
        }
    }
    return (int *) arr;
}
int arr[m][n];
//Algorithm - transpose
for (int i = 0; i < m; i++){
    for (int j = 0; j < n; j++){
        arrT[j][i] = arr[i][j];
    }
}

有没有办法做到这一点?

function至少有四个问题。

//Fill matrix with test data
int *testArrData(int m, int n){
    int arr[n][m];
    int* ptr;
    ptr = &arr[0][0];
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            *((ptr+i*n)+j) = rand()%10;
        }
    }
    return (int *) arr;
}

首先,您声明了一个可变长度数组

int arr[n][m];

可变长度 arrays 不是标准 C++ 功能。

第二个问题是这些 for 循环

    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            *((ptr+i*n)+j) = rand()%10;
        }
    }

不正确。 看来你的意思

    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            *((ptr+i*m)+j) = rand()%10;
        }
    }

您正在返回一个指向具有自动存储持续时间的本地数组的指针,该数组在退出 function 后将不再存在。 所以返回的指针是无效的。

并且 arrays 没有赋值运算符。

而是使用向量std::vector<std::vector<int>> 例如

std::vector<std::vector<int>> testArrData(int m, int n){
    std::vector<std::vector<int>> v( n, std::vector<int>( m ) );

    for ( auto &row : v )
    {
        for ( auto &item : row )
        {
            item = rand() % 10;
        }
    }

    return v;
}

您正在返回 1d 指针。如果您想返回双指针,您应该编写此语法 int ** testarrdat(.......){} 使用双指针进行 2d

这就是我将如何做到这一点。 我同意 int **,因为如果您不知道如何使用向量,这很容易理解。 此外,如果您使用结果来索引数组,则 rand() 可能会导致麻烦。 如果您不想要负数,请确保使用 abs(rand() % number) 。

        int **create_row_col_matrix(int num_rows, int num_cols, bool init_rnd)
        {
            num_rows = min(max(num_rows, 1), 1000); // ensure num_rows = 1 - 1000
            num_cols = min(max(num_cols, 1), 1000); // ensure num_cols = 1 - 1000

            int *matrix_total = new int[num_rows*num_cols];

            int **matrix_row_col = NULL;

            // out of memory
            if (matrix_total == nullptr) { /* out of memory message*/  return nullptr; }

            for (int a = 0; a < num_rows; ++a)
            {
                // initialize the 1D array of 1D pointers
                matrix_row_col[a] = &matrix_total[num_cols*a];
            }

            // assign the test data
            if (init_rnd)
            {

                for (int run_y = 0; run_y < num_rows; ++run_y)
                {
                     for (int run_x = 0; run_x < num_cols; ++run_x)
                     {
                        matrix_row_col[run_y][run_x] = abs(rand() % 10);
                     }
                 }
            }

            return matrix_row_col;
        }

int src_x = dst_y = 7;
int src_y = dst_x = 11;

int **arr_src = create_row_col_matrix(src_y, src_x, true);
int **arr_dst = create_row_col_matrix(dst_y, dst_x, false);

// only use valid memory pointers
if (arr_src && arr_dst)
{
    for (int a = 0; a < dst_y; ++a)
    {
        for (int b = 0; b < dst_x; ++b)
        {
             arr_dst[a][b] = arr_src[b][a];
        }
    }
}  

暂无
暂无

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

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