Pages: [1]   Go Down
Author Topic: Return bidimensional array  (Read 726 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone, I have a problem with two-dimensional arrays.

i have this function:
Code:
String allLoco()
{
  File locoFile = SD.open("loco.txt");
  String allMyLoco[numLoco][3];
 
  ............
 
  return allMyLoco;
}

The error is this:
LocoDatabase.h:64: error: conversion from 'String (*)[3]' to non-scalar type 'String' requested

how can I fix it?

TNK
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


you better pass a pointer/reference to the read function which fills the String[][] from disk.

Now the 2dim array is a local var in the function which will be overwritten when the next function is called
google for scope in C / C++ for the details

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply.
My purpose is read a CSV file. the problem is that the length of the array may vary (may vary the number of rows in the CSV).

There is no way to pass an array initialized inside a function?
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3359
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is no way to pass an array initialized inside a function?
You can declare an array as static within the function and pass it. Example:
Code:
char* foo() {
  static char myArray[10];
  // Do something with myArray
  return myArray;
}
...but that is because using static is functionally the same as declaring it global. Example:
Code:
char myArray[10];

void foo() {
  // do stuff with myArray
}
Using static just helps with scope so that you can only modify it within that function.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
There is no way to pass an array initialized inside a function?
If you mean "Is there no way to return an array CREATED inside a function", then the answer is no, if the array is statically defined.

However, in your case, the size of the array must be known in advance of the call, since it is used as soon as the function starts, to create the array.

The 2D array of strings is going to kill you, anyway. A 2D array of strings (or 3D array of chars) would be a lot smaller.

You need to tell us more about how many rows in the table, and if there are 3 values per row (as appears from your declaration). If you are not using a Mega, you have less than 1500 bytes of SRAM in which to hold you 2D array of Strings or strings.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For simplicity in situations like this I usually make myself a struct so I'm not messing with multidimentional arrays:

Code:
struct csv_struct {
  String foo;
  String bar;
  String meep;
};

struct csv_struct myArray[50];

Then you can access the data as

Code:
myArray[4].foo

or

Code:
myArray[43].bar
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all for your help ...
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you mean "Is there no way to return an array CREATED inside a function", then the answer is no, if the array is statically defined.

I'd say that strictly speaking that 'no' should be 'not without exploiting non-intuitive quirks of the language'; it's best not to get into those situations in the first place but technically it is possible to return a local array if it is returned within a struct.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Dynamic resizing of arrays is something which is tricky even on a full-blown PC with a proper memory management layer.

On a small microcontroller with limited RAM and no real memory management it's just not really practical.

You're much better off dealing with the CSV file in chunks - be that in single lines, or blocks of a specific size.

Personally I'd write a function which opens the file and returns a "handle" to it, plus another function which returns successive lines.  A third function which allows you to "seek" to a specific line is also useful, but not essential.

Finally, a "close" function to clean it all up and release any memory allocated is a must.

That way, instead of dealing with messy multi-dimentional arrays, you're just dealing with simple function calls.  Something like:

Code:
int myFile = csv_open("myfile.csv");
while(csv_getline(myFile,(struct myStruct *)&data))
{
  doSomethingWithData(data.xcoord,data.ycoord);
  doSomethingElse(data.name,data.speed);
}
csv_close(myFile);

as an example - of course, there is 9,000,000,000 more lines of code to be written in the csv_* functions etc smiley-wink
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: