Go Down

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

cowasaki



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.


Thanks,  as per the example I'm using a null entry to signify the last entry.

I will read the array in the add function and store the number of entries.

cowasaki


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.


Thanks, "strings" was a typo, you've pulled me up on that one before :D.  I am only looking at Strings here.....

I have tried calling the function with testpassarray(&test1[]);  and   testpassarray(&test1);

I get an error due to the type in the function....  What type should it be to receive the pointer and how do I implement it?


void testpassarray( <what type> elements){
  stored[numarrays]=elements;   // store the address in the stored[] array as a pointer to the actual array
  numarrays+=1;
  Serial.println("");
  Serial.println((unsigned long int)elements,HEX);   // was used to show the address but can be ignored
//  Serial.println(elements[0]);
//  Serial.println(elements[1]);
//  Serial.println(elements[2]);

}

and crucially once that is done how do I use the address in stored[] to actually pull the data from the original global array?

cowasaki

I really do appreciate all your help in this matter.  The test program was created just to find the answers.  I couldn't really use the actual program as it is 7000 lines of code spread across multiple files.

once I understand this using the test program I can transfer that understanding to the actual library....


PaulS

Quote
I have tried calling the function with testpassarray(&test1[]);  and   testpassarray(&test1);

Neither of which is right. It is either:
Code: [Select]
testpassarray(&test1[0]);
or
Code: [Select]
testpassarray(test1);
Both values are the same, although, to me, the second is a lot easier to see as meaning the whole array.

Quote
void testpassarray( <what type> elements){

<what type> is String *.

PaulS

Quote
and crucially once that is done how do I use the address in stored[] to actually pull the data from the original global array?

The array, stored, is a collection of pointers to arrays. That makes it a 2D array, as I mentioned earlier.

stored[ 0 ][ 0 ] is a pointer to the first element of the first array. stored[ 0 ][ 1 ] is a pointer to the second element of the first array. stored[ 1 ][ 0 ] is a pointer to the first element of the second array.

Go Up