Home > other >  Assign pointer to 2d array to an array
Assign pointer to 2d array to an array

Time:07-25

So I got a function which creates me 2D array and fill it with test data. Now I need to assign the pointer to an array

//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();
        }
    }
    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];
    }
}

Is there any way of doing this?

CodePudding user response:

There are at least four problems with the 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();
        }
    }
    return (int *) arr;
}

First of all you declared a variable length array

int arr[n][m];

Variable length arrays are not a standard C feature.

The second problem is that these for loops

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

are incorrect. It seems you mean

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

You are returning a pointer to a local array with automatic storage duration that will not be alive after exiting the function. So the returned pointer will be invalid.

And arrays do not have the assignment operator.

Instead use the vector std::vector<std::vector<int>>. For example

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

CodePudding user response:

This is how I would accomplish this. I agree with int ** because it is easy to understand if you dont know how to use vectors. Also, the rand() can cause trouble if you are using the result to index an array. Make sure to use abs(rand() % number) if you don't want negative numbers.

I've updated the answer due to some vital missing code.


// This method creates the overhead / an array of pointers for each matrix 

typedef int* matrix_cells;

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

    // overhead: create an array that points to each row
    int **martix_row_col = new matrix_cells[num_rows];

    // initialize the row pointers
    for (int a = 0; a < num_rows;   a)
    {
        // initialize the array of row 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 =  7, dst_x = 11;
int src_y = 11, dst_y = 7;

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

for (int a = 0; a < dst_y;   a)
{
    for (int b = 0; b < dst_x;   b)
    {
         arr_dst[a][b] = arr_src[b][a];
    }
}

delete matrix_src[0];   // int *matrix_total = new int[src_y*src_x]
delete matrix_src;      // int **matrix_row_col = new matrix_cell[src_y]

delete matrix_dst[0];   // int *matrix_total = new int[dst_y*dst_x]
delete matrix_dst;      // int **matrix_row_col = new matrix_cell[dst_y]

// the overhead is matrix_src and matrix_dst which are arrays of row pointers
// the row pointers makes it convenient to address the cells as [rown][coln]

CodePudding user response:

you are returning 1d pointer .if u want to return double pointer u should wriite this syntax int ** testarrdat(..........){} use double pointer for 2d

  • Related