A question on pointers...

I have searched C++ forums and tried experimenting, but I cannot find any advice on using and returning arrays with pointers and libraries.

I have a Function called 'FunctionName' in the Libary 'LibName'. I want to run a main loop which has two arrays (Array1 and Array2) of sizes 16x17 and 17x16, which takes the arrays and sends them to the function. The function then processes the Arrays and returns its own array back to the main function:

In a .pde I am calling a Function using:

#include <LibName>
LibName libname
Var=libname.FunctionName(Array1 [16][17]. Array2[17][16];

where LibName.h:

public: int FunctionName(int Array1[16][17], int Array2[17][16])

and LibName.cpp:

int LibName::FunctionName(int Array1[16][17], intArray2[17][16])
  int *ResultPointer = Result;
  
  return *ResultPointer;
int *ResultPointer = Result;

I don't see where you've declared "Result"

Its just an example of what i'm doing,

For the sake of the argument:

int Result[0]=Array1[0][0]+Array2[0][0];
Result[623]=Array1[3][7]+Array2[8][5];

The actual code is 593 lines of calculation but the gist is it outputs an array

int Result[0]=Array1[0][0]+Array2[0][0];

?

Did you have a specific question?
Is there some reason why you want to return an int pointer rather than a pointer to 16x17 array of ints? (or a struct containing a 16x17 array of ints, which at least I could write without having to resort to tools, manuals, or experiments.)

Note that with three arrays of 16x17 ints, you are getting rather close to using up all the memory you have available on a 328 (1632 of 2048 bytes...)

Its not the real code it is just showing that the function is taking the values from Array1 which is a 16x17 array and Array2 which is a 17x16 array, and doing some function to make a Third Array 'Result'

I'm not putting in the actual code as it i too long and is already working correctly. It is the pointers which I am working on.

@ westfw

I am new to pointers, all I want to do is have a function which takes two arrays and outputs an array. I cant find any guides or help on it.

If you know of how to do this, I'd gladly appreciate the help. I would like to return the values from the results array- the entire array after doing some calculations in a referenced function.

Also its using the Mega, so the RAM is not an issue

Cheers

The first thing to note, as I pointed out in your other thread, is that you are calling the function wrong.

Var=libname.FunctionName(Array1 [16][17]. Array2[17][16];

This should be:

Var=libname.FunctionName(Array1, Array2);

The function either needs to dynamically allocate space for the result array, and then return a pointer to that space, or the caller needs to allocate the space (statically or dynamically) and pass the pointer to the space to the function.

The easiest way to do it is statically:

int Array1[16][17]; // Fill this...
int Array2[17][16]; // and this...
int Output[17][17]; // Or whatever size this needs to be...

ComputeResults(Array1, Array2, &Output);

The function is defined as:

void ComputeResults(int array1[16][17], int array2[17][16], int &array3[17][17]); // Sizes need to be correct

The function is implemented:

void ComputeResults(int A1[16][17], int A2[17][16], int &Results[17][17])
{
    Results[5][6] = A1[2][3] * A2[5][4];
}

Of course, you'll need to define all elements of the array, with valid data.

Ahhh Thank you PaulS

I would never have tried that- using void as the function then using the reference of the pointer Nice.

Thank you kindly

using void as the function then using

Please, that should be:
...using void as the function return type then using...

Ahhh

one problem- it still will not compile-

.h: error: declaration of 'array3' as array of references
.cpp: error: declaration of 'Results' as array of references

Now that I'm awake, and have re-read the relevant section on passing arrays to functions, I see that arrays are, by default, passed by reference, so the & symbol, to force pass by reference, is not needed.

Thanks for the help, but its still not compiling- it doesnt seem to like the definitions. This is the code I'm using

.pde:

int Array1[2][3]= { 
  1,1,1,
  1,1,1};

int Array2[3][2]= { 
  1,1,
  1,1,
  1,1};
  
int Output[3][3]={
  0,0,0,
  0,0,0,
  0,0,0}; 

#include <Test.h>

TestClass testclass

void setup(){ 
  Serial.begin(9600); 

  int Out=testclass.ComputeResults(Array1, Array2, Output);

} 


void loop(){
}

Test.h

#ifndef Test_h

#define Test_h

#include "WProgram.h" //Standard librarys



class TestClass //Declare a Grid Solver 
{

public:

void ComputeResults(int array1[][3], int array2[][2], int array3[][3]); // Sizes need to be correct 

private:
};

#endif

Test.cpp:

#include "WProgram.h" //Include standard library
#include "Test.h" //Include relevent header file


//--------------------------------------------------------------------------------------

void TestClass::ComputeResults(int A1[2][3], int A2[3][2], int Results[3][3])
{
    Results[2][2] = A1[1][2] * A2[2][1];
}

Knowing me its probably something really simple. I haven't used libraries or pointers before as you can probably guess.

What reference are you using, if it has this kind of information in would you recommend getting it?

I don't know what kind of errors you are getting, but you have some basic syntax issues.

TestClass testclass

This needs to have a ; at the end.

int Out=testclass.ComputeResults(Array1, Array2, Output);

The ComputeResults method of TestClass is declared with a return type of void, which means that it doesn't return a value. You can't store the results of a function that does not return anything in a variable of any type.

After fixing these problems in the sketch, what other errors are you getting?

o: In function `TestClass::ComputeResults(int (*) [3], int (*) [2], int (*) [3])':
C:\Users\ROBERT~1\AppData\Local\Temp\build6902218517303846712.tmp/Test.cpp:21: multiple definition of `TestClass::ComputeResults(int (*) [3], int (*) [2], int (*) [3])'

C:\Users\ROBERT~1\AppData\Local\Temp\build6902218517303846712.tmp\Test\Test.cpp.o:R:\arduino-0018\arduino-0018\libraries\Test/Test.cpp:21: first defined here

C:\Users\ROBERT~1\AppData\Local\Temp\build6902218517303846712.tmp\core.a(main.cpp.o): In function `main':

R:\arduino-0018\arduino-0018\hardware\arduino\cores\arduino/main.cpp:7: undefined reference to `setup'

R:\arduino-0018\arduino-0018\hardware\arduino\cores\arduino/main.cpp:10: undefined reference to `loop'

I have searched C++ forums and tried experimenting, but I cannot find any advice on using and returning arrays with pointers and libraries.

Look at this:
http://www.student.cs.uwaterloo.ca/~cs343/documents/refvsptr.html

What reference are you using, if it has this kind of information in would you recommend getting it?

"The C Programming Language" by Kernighan And Ritchie is THE reference for C language

TestClass testclass

Here's a spare - ';'

Thanks for the link and book ,will read over

I added in the semicolon, but it still gave that error output.

For some reason the code still doesn't work, and the only difference form the examples I can think of is the use of libraries, which makes no sense.

K&R doesn't have anything on references, because they don't exist in C, apart from the implicit ones.
I really can't recommend the corresponding book on C++ by Stroustrup, because it's largely illegible!

One of the best is Kelley & Pohl's "A book on C", and although it has a chapter or two on C++, I can't remember if it has anything on references.

I copied the sketch, header file, and source file that you posted. It compiled fine for me.