The following code reads a txt file from a SD card, finds the corresponding character and prints the next 12 characters:
...
char temp;
byte who = random(1, 13); // 1 to 12
temp = findName(who);
Serial.print(who);
Serial.print(" ==> ");
Serial.println(temp);
...
char findName (byte number) {
char text[12];
byte i;
txt = SD.open("test.txt");
txt.seek(12 * (number - 1));
for (i = 0; i < 11; i++) text[i] = txt.read();
txt.close();
return text;
}
But the following, on a LCD, does not. (setCursor, etc. ommited for clarity)
...
char temp;
byte who = random(1, 13); // 1 to 12
temp = findName(who);
lcd.print(who);
lcd.print(" ==> ");
lcd.print(temp);
...
I'm certain the cause is a variable data type mismatch, but I don't know how to solve it. Any help is appreciated.
Thank you.
1 Answer 1
In the findName()
function, you declare a char array of name text
. Internally, text
is a pointer to a block of memory 12 bytes in size.
When you return text
within the findName()
function, you are passing back the pointer to the block of memory. The return type of char
is inconsistent with the return command. But probably more problematic is that once findName()
ends, the memory set aside for the text
array is marked as unused. It is no longer reserved for holding anything and may be reallocated at any time by some other function or operation.
In C/C++, array variables do not refer to the entire array, but to the first element in the array. The fact that your program works with Serial.print()
is probably just good luck. lcd.print()
may declare some variable or buffer which overwrites what the findName()
function had put in those 12 bytes.
There are many approaches to take to resolve this. One way is to declare char arrays in the "highest" scope in which it will be used, and to declare it with the largest size that you could ever see in the operation of your program.
In your case, you can:
...
char temp[12];
byte who = random(1, 13); // 1 to 12
findName(temp, who);
Serial.print(who);
Serial.print(" ==> ");
Serial.println(temp);
...
void findName (char *text, byte number) {
byte i;
txt = SD.open("test.txt");
txt.seek(12 * (number - 1));
for (i = 0; i < 11; i++) text[i] = txt.read();
txt.close();
}
What this does:
temp
is now the char arrayfindName()
takes the pointer to the char array, and the number as argumentsfindName()
modifiestext
, which refers to the same block of already-allocated memory astemp
, and that block of memory does not go away at the end offindName()
.- Because
findName()
is not returning a value, it's return type isvoid
.
-
Thank you, sir! Learned a lot from your answer and your snippet finally solved the issue. Now I can finally proceed. Thanks again.ghpg2– ghpg22019年01月28日 19:23:17 +00:00Commented Jan 28, 2019 at 19:23
lcd
lcd
type and library?The variable is a char array
... so, why would a char array have aprint
method?