I understand how to pass an array to a function, the following code shows how:
String test1[10];
String test2[10];
void setup() {
// put your setup code here, to run once:
test1[0]="1";
test1[1]="2";
test1[2]="3";
test2[0]="one";
test2[1]="two";
test2[2]="three";
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
testpassarray(test1);
testpassarray(test2);
}
void testpassarray(String *elements){
Serial.println(elements[0]);
Serial.println(elements[1]);
Serial.println(elements[2]);
}
What I need to do is STORE the pointer in an array so:
(I know the program is pointless but I need to understand the concept!
String test1[10];
String test2[10];
unsigned long int stored[100];
int numarrays=0;
void setup() {
// put your setup code here, to run once:
test1[0]="1";
test1[1]="2";
test1[2]="3";
test2[0]="one";
test2[1]="two";
test2[2]="three";
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
testpassarray(test1);
testpassarray(test2);
}
void testpassarray(String *elements){
stored[numarrays]=elements;
numarrays+=1;
Serial.println(elements[0]);
Serial.println(elements[1]);
Serial.println(elements[2]);
}
The code does not work. I'm not at all new to programming just haven't got a big history of this language!
Also how can I tell how many elements inside the passed array?
Is the type "uint16_t" the correct one? If so how do I store the address of the passed array so that I can use it in another function later?
Thanks all.
ADDED:
The following code shows what I am trying to do (JUST FOR THIS EXAMPLE) but clearly several lines are just rubbish
The lines which start "//////" are the ones I probably need help with.
I know I could call the original function again BUT this is not what I am trying to do. I need to understand HOW to do what this program is doing in order to add to an existing very large program. So I have created this example just as a learning tool.
String test1[10];
String test2[10];
////// uint16_t stored[100]; // HOW ????
int numarrays=0;
void setup() {
// put your setup code here, to run once:
test1[0]="1";
test1[1]="2";
test1[2]="3";
test2[0]="one";
test2[1]="two";
test2[2]="three";
Serial.begin(115200);
testpassarray(test1);
testpassarray(test2);
reprint(0);
reprint(1);
}
void loop() {
// put your main code here, to run repeatedly:
}
void testpassarray(String *elements){
///// stored[numarrays]=elements; // HOW ????
numarrays+=1;
Serial.println(elements[0]);
Serial.println(elements[1]);
Serial.println(elements[2]);
}
void reprint(unsigned long int whicharray){
///// setup variable "elements" // HOW ????
///// elements= address of which array // HOW ????
Serial.println(elements[0]);
Serial.println(elements[1]);
Serial.println(elements[2]);
}