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 array
findName()
takes the pointer to the char array, and the number as arguments
findName()
modifies text
, which refers to the same block of already-allocated memory as temp
, and that block of memory does not go away at the end of findName()
.
- Because
findName()
is not returning a value, it's return type is void
.
lcd
– Jaromanda X Jan 28 '19 at 3:34lcd
type and library? – Juraj Jan 28 '19 at 6:10The variable is a char array
... so, why would a char array have aprint
method? – Jaromanda X Jan 28 '19 at 8:48