-1

I'm trying to create an array of pointers to a 2d array of float, I tried this :

float (*arrayOfPointers[3][12])[10];

but when I do : arrayOfPointers[0] = anArray;

I get an error message.

Would be nice if someone tried to help with that.

Thanks,

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • Arrays are not copyable or assignable using `=`. Arrays need to be copied element-by-element in a loop. – PaulMcKenzie Oct 04 '20 at 01:15
  • Yes, but isnt the name of an array supposed to give the address of the first element of the array ? –  Oct 04 '20 at 01:17
  • @VladNovakovsky Didnt get what you mean, sorry im new to this. Do you know how I can create an array of pointers that point to a double array of float ? –  Oct 04 '20 at 01:19
  • Do you want each element of array of pointers point to separate 2D array? – Vlad Oct 04 '20 at 01:21
  • Yes exactly ! I have ten specific 2d arrays, and I want to create an array of pointers, that point to each one of this two 2D arrays. Is it possible ? Thank you –  Oct 04 '20 at 01:22
  • Try using typedef - it's easier to read. This do not give error. typedef float A2D[2][3]; A2D a1 = {{1, 3, 0}, {-1, 5, 9}}; A2D a2 = {{1, 3, 0}, {-1, 5, 9}}; A2D * arrayOfPointers[2] = {a1, a2}; arrayOfPointers[0]; – Vlad Oct 04 '20 at 01:34
  • @Omar El Metoui you were very responsive when you asked for help. community spends time giving answers. Would be polite to give feedback –  Oct 04 '20 at 19:49

1 Answers1

0

To access elements of array you need to allocate memory: either on stack(like below) or in static memory (globals, syntax the same as for stack) or in heap (using new operator. Then do not forget to delete memory in the end of usage of array. Or use smart pointers). Using std::vector is less error prone.

    typedef float A2D[2][3]; // declaration of type
    A2D a1 = {{42, 3, 0}, {-1, 5, 9}}; // definition of 2D array with initialization
    A2D a2 = {{1, 3, 0}, {-1, 5, 9}};
    A2D * arrayOfPointers[2] = {&a1, &a2}; // definition of array of pointers to 2D arrays with initialization
    float f1 = (*(arrayOfPointers[0]))[0][0]; // accessing element
    float f2 = (*(arrayOfPointers[0]))[0][1];
    float f3 = (*(arrayOfPointers[0]))[1][1];
    assert(f1 == 42);
    assert(f2 == 3);
    assert(f3 == 5);

If you need to define dimensions of array in run time or lifecycle of array is longer than lifecycle of function where you define array then you need to allocate array in heap.

A dynamic 2D array is basically an array of pointers to arrays. You can initialize it using a loop.

int rowCount = 2;
int colCount = 3;
float** da1 = new float*[rowCount];
for(int i = 0; i < rowCount; ++i)
    da1[i] = new float[colCount];

for(int i = 0; i < rowCount; ++i)
{
    for(int j = 0; j < colCount; ++j)
    {
        da1[i][j] = a1[i][j];
    }
}


float** da2 = new float*[rowCount];
for(int i = 0; i < rowCount; ++i)
    da2[i] = new float[colCount];

for(int i = 0; i < rowCount; ++i)
{
    for(int j = 0; j < colCount; ++j)
    {
        da2[i][j] = a2[i][j];
    }
}

float** arrayOfPointersDyn[2] = {da1, da2};
{
    float f1 = (arrayOfPointersDyn[0])[0][0];
    float f2 = (arrayOfPointersDyn[0])[0][1];
    float f3 = (arrayOfPointersDyn[0])[1][1];
    assert(f1 == 42);
    assert(f2 == 3);
    assert(f3 == 5);
}
Vlad
  • 1,977
  • 19
  • 44