I have a question. I have a 2d char array and I want to print a port of it with Serial.println() in a function with a variable as arguement.
char (*text)[6] = {
{
"configuring component\n"
"1 = turnout\n"
"2 = memory\n"
"3 = detector"
},{
"select ID"
},{
"select IO"
},{
"Adjust curved switch position\n"
"use 0-9, - and + to adjust the position\n"
"send 's' if position is correct"
},{
"Adjust straight switch position\n"
"use 0-9, - and + to adjust the position\n"
"send 's' if position is correct"
},{
"press Y/n to store/discard the object"
}
} ;
static void printState(byte i) {
while(*text[i] != 0) {
Serial.write(*text[i]);
*text[i]++; } } // N.B. I have not yet tried to compile this because reasons
I believe this might work but I have no clue to be honest. I am also not content with the while loop. I copied it from a function from work. But I should be able to use the Serial.print function.
I understand pointer to this extend. I am puzzled much by pointers to 2D arrays. Can anybody give me a hand with solving this puzzle?
What about a simple Serial.print(text[0])
bask185:
I have a 2d char array ....
char (*text)[6] ;
No, you have an array of pointers to chars, that's a different beast.
Which type of Arduino are you using?
The strings reside in Flash and RAM , which is rather precious on smaller Arduinos.
Whandall:
No, you have an array of pointers to chars, that's a different beast.
OP doesn't even have that. If you drop his/her variable definition / initialization into a proper Arduino sketch, it won't compile:
char (*text)[6] = {
{
"configuring component\n"
"1 = turnout\n"
"2 = memory\n"
"3 = detector"
}, {
"select ID"
}, {
"select IO"
}, {
"Adjust curved switch position\n"
"use 0-9, - and + to adjust the position\n"
"send 's' if position is correct"
}, {
"Adjust straight switch position\n"
"use 0-9, - and + to adjust the position\n"
"send 's' if position is correct"
}, {
"press Y/n to store/discard the object"
}
};
void setup() {
}
void loop() {
}
Arduino: 1.8.5 (Windows 10), TD: 1.44, Board: "Arduino/Genuino Uno"
sketch_aug21b:22: error: scalar object 'text' requires one element in initializer
};
^
exit status 1
scalar object 'text' requires one element in initializer
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Whandall:
No, you have an array of pointers to chars, that's a different beast.
Which type of Arduino are you using?
The strings reside in Flash and RAM , which is rather precious on smaller Arduinos.
O yeah, I forgot about that small detail. At work I program a thing with 32k RAM which is like 'unlimited variables space to us. We tried to fill it all, but we only reached 8k at most. At home I program an Uno.
So it's best to go back to simple Serial.println("with hard coded texts");
septillion:
What about a simple Serial.print(text[0])
Makes perfect sense to me. That does the exact same thing as my while loop from my old print function.
I had the impression the brackets would not matter, but they do.
const char *text[6] = {
"configuring component\n"
"1 = turnout\n"
"2 = memory\n"
"3 = detector",
"select ID",
"select IO",
"Adjust curved switch position\n"
"use 0-9, - and + to adjust the position\n"
"send 's' if position is correct",
"Adjust straight switch position\n"
"use 0-9, - and + to adjust the position\n"
"send 's' if position is correct",
"press Y/n to store/discard the object"
} ;
void printState(byte i) {
Serial.print(text[i]);
}
void setup() {
Serial.begin(250000);
for (byte i=0; i<sizeof(text)/sizeof(text[0]); i++) {
Serial.print(F("State "));
Serial.print(i);
Serial.print(F(" = \""));
printState(i);
Serial.println(F("\"\n"));
}
}
void loop() {}
(the following was copied from the Serial monitor using ctrl-C )
State 0 = "configuring component
1 = turnout
2 = memory
3 = detector"
State 1 = "select ID"
State 2 = "select IO"
State 3 = "Adjust curved switch position
use 0-9, - and + to adjust the position
send 's' if position is correct"
State 4 = "Adjust straight switch position
use 0-9, - and + to adjust the position
send 's' if position is correct"
State 5 = "press Y/n to store/discard the object"
char (*text)[6] = {
That is a pointer to an array of six characters. The parens cause the indirection to happen first.
char *text[6] = {
This is an array of six pointers to characters.
If you REALLY want a 2D array of characters, every row has to be the same length:
const char text[6][104] =