0

I'm trying to fill an array with random numbers using pointers. Here's my code so far:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

const int mRows = 3;
const int mCols = 5;

void fillMatrix(int ** m_ptr_ptr, int, int);

int main()
{
    unsigned seed;
    seed = time(0);
    srand(47);

    int matrix[mRows][mCols];
    int* matrix_ptr[mRows];
    int** matrix_ptr_ptr = &matrix_ptr[0];


    for (int i = 0; i < mRows; i++)
    {
        matrix_ptr[i] = &matrix[i][0];
    }


    fillMatrix(matrix_ptr_ptr, mRows, mCols);

    cout << endl << endl;

    for (int j = 0; j < mRows; j++)
    {
        for (int k = 0; k < mCols; k++)
        {
            cout << setw(9) << *((*matrix_ptr_ptr + j) + k) << setw(6);
        }
            cout << endl << endl;
    }
}

void fillMatrix(int **matrix_ptr_ptr, int N, int P)
{
    for (int j = 0; j < N; j++)
    {
        cout << left;
        for (int k = 0; k < P; k++)
        {
            *((*matrix_ptr_ptr + j) + k) = rand() % 25;
            cout << setw(9) << *((*matrix_ptr_ptr + j) + k) << setw(6);
        }

        cout << endl << endl;
    }
}

When I print the matrix using the fillMatrix function I get the following

17 24 11 0 20

13 3  0 13 22

20 21 11 19 18

After printing out the matrix_ptr_ptr in main by using the for loop in main I get:

17 13 20 21 11

13 20 21 11 19

20 21 11 19 18

How can I get matrix_ptr_ptr in main to equal the matrix outputted by the fillMatrix function? Any help would be appreciated

  • 1
    Do note you can make you life a lot easier by using a single dimension vector and abstract it behind a matrix class. Then you can overload the `operator ()` to access it and fake that it has multiple dimensions. It makes it a lot easier to pass it around. – NathanOliver Mar 13 '17 at 17:09
  • 1
    consider std::generate() with a random number generator. its better :) – jonas_toth Mar 13 '17 at 17:12
  • 1
    as noted by nathan, dont use raw pointers. Matrix operations are so common, there exist many libraries to deal with them. i know of Eigen and Blaze, but there is more out there. then use the STL (generate) and particulary your dereferencing into the matrix is more complicated then necessary. – jonas_toth Mar 13 '17 at 17:15
  • @jonas_toth There is nothing wrong with non owning raw pointers which the OP is using. It is just raw arrays do lend themselevs to be easily passed around like a `std::vector` or `std::array`. – NathanOliver Mar 13 '17 at 17:17
  • @NathanOliver true. but `int **matrix_ptr_ptr, int N, int P` is problematic. its the ptr, length interface bjarne talks about. according to the gsl, a pointer should always point to a single element, not an array (nor a matrix). https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-use-ptr – jonas_toth Mar 13 '17 at 17:19

1 Answers1

-2
*((*matrix_ptr_ptr + j) + k) = rand() % 25;

looks like you need to do this instead:

*(*(matrix_ptr_ptr + j) + k) = rand() % 25;

but even better(pointed out in comment!)

 matrix_ptr[j][k]

You should dereference after applying the offset. But dont write matrix stuff yourself anyway, as pointed out in the comments, use a library. They can do this more efficient and better. And its faster to program as well.

Eigen, Blaze or others should fullfill your needs.

jonas_toth
  • 709
  • 4
  • 8