1

For the heads up: I am incredibly new with cpp. Being used to PHP and JS as work languages, I find pointers incredibly confusing.

So I have this Class called Macierz. The class is supposed to hold Matrixes of float variables and ought to have a constructor accepting an 2d array in order to print them into the field.

The field is declared like this float mx3[3][3];

And the constructor has such declaration: Macierz(float**); With the body using an additional function:

Macierz::Macierz(float** f) {
    length = 3;
    populateWith(f, length);
}

void Macierz::populateWith(float** val, int length) {
    for (int i = 0; i < length; i++)
        for (int j = 0; j < length; j++)
            mx3[i][j] = val[i][j];
}

In my main() function I want to declare the class with a created float array. I try to do it as so, but It just won't work the way God intended:

float y[3][3] = { {1.00f, 2.00f, 3.00f}, { 4.00f, 5.00f, 6.00f }, { 7.00f, 8.00f, 9.00f } };

Macierz m5(y);

This is the base template of what I want to do. I've tried making the y variable a double pointer, a regular pointer, passing via reference and it just won't kick.

What would be the most prober way to pass this variable?

Any help will be amazing, I am really a noob in this language.

aln447
  • 981
  • 2
  • 15
  • 44
  • 1
    An array of arrays is not equivalent to a pointer to pointer. See e.g. [this old answer of mine](https://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456) for a simple drawing of the difference. – Some programmer dude Apr 12 '18 at 07:50
  • And to help you I suggest you stat using the standard library and its [containers](http://en.cppreference.com/w/cpp/container). In your case, since the arrays is of a compile-time fixed size you could use [`std::array`](http://en.cppreference.com/w/cpp/container/array). Something like `std::array, 3>, 3> y = { ... }`. Then just pass references to the type as arguments and copy it as one would normally copy a variable (using assignment). – Some programmer dude Apr 12 '18 at 07:53
  • @Someprogrammerdude For now the project has many elements in it using the float[][] type thing, therefore changing the base way it works would add alot more work into it. If I had the chance now, Id do it with Vectors probably :P But thanks for the help – aln447 Apr 12 '18 at 07:57

2 Answers2

1

You need to remember that arrays naturally decays to pointers to their first element. But that isn't recursive.

For y in your example, the decay is to a pointer to the first element of y, which is equal to &y[0]. That is a pointer to an array, and will have the type float(*)[3], which is the type you need for your arguments:

Macierz::Macierz(float (*f)[3]) { ... }
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks. I have tho found another way by rewriting the table into a pointer to pointers one. Check my answer below. Turned out to be quite of a basic answer :) – aln447 Apr 12 '18 at 08:06
0

Managed to fix this by copying the float into a pointer-to-pointer types

float y[3][3] = { {1.00f, 2.00f, 3.00f}, { 4.00f, 5.00f, 6.00f }, { 7.00f, 8.00f, 9.00f } };

    float** x = 0;
    x = new float*[3];

    for (int i = 0; i < 3; i++) {
        x[i] = new float[3];

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

    Macierz m5(x);
aln447
  • 981
  • 2
  • 15
  • 44
  • 2
    Don't forget to `delete[]` the memory you `new[]`. Its also an extra step which of course will have an impact on performance. And you don't need the actual copying of the values, just do e.g. `x[i] = y[i]` in the outer loop. – Some programmer dude Apr 12 '18 at 08:21
  • Sure. Thank you! – aln447 Apr 12 '18 at 08:47