• Create Account

## Converting Arrays: float[][] to float**

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1jdub  Members

459
Like
0Likes
Like

Posted 09 January 2013 - 05:07 PM

Hi I am trying to compile this code:

void mult_arrays(const float **src1, const float **src2, float **output)
{
//for every element in the output array, multiply
for (int y = 0; y < 10; y++) {

for (int x = 0; x < 10; x++) {

output[y][x] = 0.0f;

for (int i = 0; i < 10; i++)
output[y][x] += src1[y][i]*src2[i][x];
}
}
}

int main(void)
{
//part 2
float arr1[10][10] =
{
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
};

float arr2[10][10] =
{
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f,},
};

float output[10][10];

mult_arrays(arr1, arr2, output);
return 0;
}

The error occurs when I call mult_arrays(): "float (*)[10]" is incompatible with parameter of type "float **"

1) Why does this error occur?  2) How can it be fixed?

Thankyou

J.W.

### #2Brother Bob  Moderators

10109
Like
0Likes
Like

Posted 09 January 2013 - 05:26 PM

The core of the problem is that arrays are not pointers, nor are pointers arrays. Therefore, if you have a float[10][10], then the array itself consists of ten arrays of ten elements each. However, the parameters to the function expects a pointer that points to an array of pointers, which in turn points to individual arrays. In short, in main(), arr1[0] is an array of ten floats, but in mult_arrays(), src1[0] is a pointer, and the two are incompatible.

The solution depends on what you expect. Seeing that your code assumes arrays of fixed size, you could make the parameters of fixed-size arrays too.

void mult_arrays(const float src1[10][], const float src2[10][], float output[10][])
{ ... }

All but the last dimension of multi-dimensional arrays must be static, so it's OK to leave out the last dimension since it doesn't do anything anyway in the parameter list. Alternatively, if you use C++, you can use references to arrays.

void mult_arrays(const float (&src1)[10][10], const float (&src2)[10][10], float (&output)[10][10])

The last dimension is required now since the complete type has to be encoded into the reference type.

### #3ultramailman  Prime Members

1720
Like
0Likes
Like

Posted 09 January 2013 - 06:22 PM

Try float (*src)[] instead. src will be a pointer to arrays, instead of pointer to pointers.

This will work if you know the size of the 2nd dimension at compile time, so something like float (*src)[10], and not
float (*src)[aVariableSize]

Edited by ultramailman, 09 January 2013 - 06:27 PM.

### #4wintertime  Members

3826
Like
0Likes
Like

Posted 10 January 2013 - 05:58 AM

Theres also the possibility to prepare a pointer array before calling the function, which would give you the freedom to not only use it on size 10.

Though it looks like you are coding a matrix multiply and there are probably some premade matrix libs if you google for them.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.