Why not possible 'TFTscreen.text(analogRead(A0), 0, 20);' for a value on TFT ?

I have zero programming skills, but last week I got hands of my first ever Arduino Uno with a 1.17" TFT
to interface it with my current RF-Antenna project.
I managed to successfully write the 'TFTDisplayText' tutorial example into Uno.

However, the thing that has puzzled me really, despite the hours I spent trying to learn about it on the internet, is these consecutive expressions:

String sensorVal = String(analogRead(A0));
sensorVal.toCharArray(sensorPrintout, 4);
TFTscreen.text(sensorPrintout, 0, 20);

As I only want to display on TFT the value of analogRead(A0), which for Arduino is a number between 0 and 1023, why can't someone just replace the code above with the following?

TFTscreen.text(analogRead(A0), 0, 20);

Also, what's the purpose of number '4' after the 'sensorPrintout' word on line 2?

Thanks and sorry for my brevity.

cplia:
However, the thing that has puzzled me really, despite the hours I spent trying to learn about it on the internet, is these consecutive expressions:

String sensorVal = String(analogRead(A0));

sensorVal.toCharArray(sensorPrintout, 4);
TFTscreen.text(sensorPrintout, 0, 20);

Let's look at it line by line: (note that this has a lot of links to the appropriate reference pages.)

The first line calls analogRead() which reads an analog input and returns an integer value. This value is passed as an integer to the String object constructor String() which has many variations: the one that is called is the one that takes an integer parameter, which it then converts into a character string. This new String is then assigned to a variable named sensorVal.

At this point, we have read the analog input, converted the integer value into a character String object, and assigned it to sensorVal.

The next statement calls the toCharArray() function of the sensorVal object (a String object) which copies the characters in the String object into a character array, converting it into a C style character string. (Note that I'm using a lower case s, as opposed to the upper case S in the String object -- these are two very different data types.) The argument 4 in the toCharArray() call says that up to four characters can be copied into the sensorPrintout character array. It is assumed that sensorPrintout was previously declared with at least 5 characters, so it can hold those 4 characters plus a terminating NULL character that is required for C style string arrays. So the declaration was probably something like "char sensorPrintout[5];"

So now we have converted the analog reading (an integer in the range of 0 to 1023) into a C style character string (with a value from "0" to "1023")

The third statement calls the text() function of the TFTscreen object, passing in the character string and display coordinates. This causes the string to be shown on the screen.

As I only want to display on TFT the value of analogRead(A0), which for Arduino is a number between 0 and 1023, why can't someone just replace the code above with the following?

TFTscreen.text(analogRead(A0), 0, 20);

[[/quote]

The problem is one of data types. AnalogRead() returns an integer. TFTscreen() wants a character string. While an integer and character string may look similar when they are displayed, they are very different data types. TFTscreen.text() doesn't know what to do with an integer. You need to do something to convert the integer from AnalogRead() into a character string that TFTscreen.text() can understand. The sequence of statements you quoted is one way (of many) to do that.](toCharArray() - Arduino Reference)

Try this

  char buffer[5];                    //a char array to put the string in including space for the terminating NULL
  itoa(analogRead(A0), buffer, 10);  //convert the reading to a string and put it in the buffer using base 10
  TFTscreen.text(buffer, 0, 20);     //output the string to the screen

ShapeShifter:
...TFTscreen.text() doesn't know what to do with an integer. You need to do something to convert the integer from AnalogRead() into a character string that TFTscreen.text() can understand...

ShapeShifter, many thanks for the clear explanations, appreciated. I am taking your answer as a mini homework to consider for the next few days.

UKHeliBob:
Try this

  char buffer[5];                    //a char array to put the string in including space for the terminating NULL

itoa(analogRead(A0), buffer, 10);  //convert the reading to a string and put it in the buffer using base 10
 TFTscreen.text(buffer, 0, 20);     //output the string to the screen

Thanks for this too. I will give it a go today.