Passing 2D Array of Float to a Function

Hi all,
I have a feeling this is a fairly simple question but I’ve been searching and banging my head against the wall for a few hours now and I haven’t gotten anywhere so help would be greatly appreciated.

So what I’m trying to do is pass a matrix to a function that prints it out. Pretty simple, I know, but I can’t get it to work. I’ve got some other functions that multiply and make sub matrices as well but I’m hoping if I get this working I’ll be able to get the others working on my own. I should say that this code is for a leaflabs maple but the overall function should be the same. Replace SerialUSB.print with Serial.print and all is well in the world.

Here is the code I have so far.

This block of code defines mf matrix and then calls the printMatrix function. the loop() function calls this function.

void testFunction(){
	float testMatrix[3][3] = {{2.0f,3.0f,4.0f},
				{5.0f,6.0f,7.0f},
				{8.0f,9.0f,1.0f}};
	float *ptestMatrix = &testMatrix;
	SerialUSB.print("TEST FUNTION INITIATED: \n The purpose of this function is to test all math functions on the microcontroller");
	delay(2000);
	SerialUSB.println("Commence testing: Matrix printing>>>\n\n");
	SerialUSB.println("The following matrices will be used in the demonstration:");
	SerialUSB.println("MATRIX 1:");
	printMatrix(ptestMatrix, 3, 3);
	delay(5000);
}

Now the code that prints the matrix:

void printMatrix(float *_m, int noRows, int noCols){
	SerialUSB.println("Matrix Data:");
        int count = 0;
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
		SerialUSB.print(*(_m + count*sizeof(float)));
	        SerialUSB.print(", ");
                count++;
		}
		SerialUSB.println(' ');
	}
}

The output I’m getting right now is the diagonal of the matrix, followed shortly by the uC crashing and it is driving me nuts. Is there a way to pass in my array to my print function, but still access it like myArray{j][k]?

Thanks in advance,

Nick

		SerialUSB.print(*(_m + count*sizeof(float)));

When you increment a pointer, it automatically increments by the number of bytes required to point to the next element of whatever type the pointer points to.

When you try to help the compiler figure out how much to increment the pointer by, you are “helping” it leap past the end of your array.

	float *ptestMatrix = &testMatrix;

No necessary. You can pass an array wherever a function calls for a pointer.

On the other hand, an 2D array needs a pointer to a pointer to reference the array, since a 2D array is an array of arrays. If ptestMatrix is to be defined as a pointer to textMatrix, it should be of type float **.

Thanks that works perfectly!

One more question though, is there an easy way to access it in x,y form?

Ex, in my main function I have array{x]{y], is there a way to pass it into my secondary function so that I can access it the same way? As it is right now I'm accessing it as *(_arrayPointer + x*noColumns + y)

Nick

If you declare the function argument to be of type float [n][], where n is a constant, then you can use normal array subscripting in the function. If n is a variable, you must calculate the array position yourself.