Go Down

Topic: Storing an array pointer in an array! (Read 2 times) previous topic - next topic

PeterH


What I am trying to do is allow the user to pass a function:

addCycleStringButton(word x,word y,word xs,word ys,long colour,long borcolour,long textcolour,long presscolour,long presstextcolour,byte borwidth,String top,word xo,word yo,int font,int initialstate,String* elements,int cyclexo,int options,bool visible,int URN);
 
Where the variable passed in red is an array of elements for a drop down list.


You need to provide your function with a way to know how many items are in the array. From what you have said so far, your code does not know this already so the caller needs to prove the information in some form. The two options I can see are (1) add an argument to receive the number of items in the array, or (2) require that the array have some special value such as a null pointer in the last position that your library code can detect.

You also need to decide whether you are going to create a copy of the array that was passed in, or merely remember the pointer you received. If you only store the pointer then you rely on the caller retaining the array intact for as long as you need it and this introduces the risk of some nastybugs if the caller gets it wrong; if you take copy the array, obviously this needs extra memory.

Implementing any of these approaches is simple enough once you have decided which approach you want to take.
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

#24
May 27, 2013, 03:06 pm Last Edit: May 27, 2013, 03:08 pm by PaulS Reason: 1
Quote
1) So how do I pass the address of the string array to the function?

You don't have a string array. You have a String array. The differences are crucial. The address of the array and the address of the first element in the array are, by definition, the same thing. You can use either &test1[ 0 ] or simply test1.

Quote
2) If I have the String array address how do I access the array itself?

Serial.println(test1[ 0 ]);
Serial.println(test1[ 1 ]);
etc.

When you store the pointer to a one dimensional array in a one dimensional array, you are creating a 2D array.

elements[ 0 ][ 0 ] is the same as test1[ 0 ] if elements[ 0 ] is pointing to test1.

cowasaki


Quote
OK, just realised what you are saying and that the array is created as a local variable and passed rather than the address.....

That's true here:
Code: [Select]
void setup() {
test1[0]="1"; test1[1]="2"; test1[2]="3";
test2[0]="one"; test2[1]="two"; test2[2]="three";
Serial.begin(115200);

  testpassarray(test1);


It's not true here:
Code: [Select]
String test1[10] = {"1","2","3"};
String test2[10] = {"one","two","three"};
String *stored[100];
int numarrays=0;

void setup() {
  Serial.begin(115200);
  testpassarray(test1);


The first code pass a pointer to the local array to the function.
The second code passes a pointer to the global array to the function.

Quote
So how can I pass the address of the global variable to the store function

Use the second code, there the array variable IS a global variable.

Quote
How do I access the Strings of the global array whilst inside either function when I have the address?

Just like you are doing in the second code.



I don't understand how version 1 and version 2 are not the same.  Both of the arrays are created globally I just stick the contents into the already created array in the original version and stick them in at the creation time in the second one.

1) So how do I pass the address of the string array to the function?
2) If I have the String array address how do I access the array itself?

PaulS

Quote
OK, just realised what you are saying and that the array is created as a local variable and passed rather than the address.....

That's true here:
Code: [Select]
void setup() {
test1[0]="1"; test1[1]="2"; test1[2]="3";
test2[0]="one"; test2[1]="two"; test2[2]="three";
Serial.begin(115200);

  testpassarray(test1);


It's not true here:
Code: [Select]
String test1[10] = {"1","2","3"};
String test2[10] = {"one","two","three"};
String *stored[100];
int numarrays=0;

void setup() {
  Serial.begin(115200);
  testpassarray(test1);


The first code pass a pointer to the local array to the function.
The second code passes a pointer to the global array to the function.

Quote
So how can I pass the address of the global variable to the store function

Use the second code, there the array variable IS a global variable.

Quote
How do I access the Strings of the global array whilst inside either function when I have the address?

Just like you are doing in the second code.

cowasaki

#21
May 27, 2013, 02:45 pm Last Edit: May 27, 2013, 02:48 pm by cowasaki Reason: 1
OK, just realised what you are saying and that the array is created as a local variable and passed rather than the address.....

So how can I pass the address of the global variable to the store function

AND

How do I access the Strings of the global array whilst inside either function when I have the address?

I tried:

testpassarray(&test1);

but it doesn't like it.....

Go Up