• Advertisement
Sign in to follow this  

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

This topic is 1838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement