Dear community,
I do not understand why the following code prints out:
&error = 200
int httpsCheck = 200;
char faultCode[30];
void setup() {
Serial.begin(74880);
}
void loop() {
sprintf(faultCode, "&error = %i", httpsCheck);
Serial.println(faultCode);
delay(1000);
}
There is no pointer used so how can Serial.println() print the whole char array?
Thank you.
Kind regards,
Niek
Does printf covert the char array into a char*?
The name faultCode is a pointer.
It is equivalent to &faultCode [0]
Juraj
5
the printf terminates the string with 0. the println prints the characters from array until the 0. that is C string
I thought char*text is the definition of a String in C and char text[] is a character array in C.
Does the println() function take in a pointer as parameter?
westfw
8
In c and c++, an array name used without an index is automatically treated as a pointer.
What were you expecting the Serial,println() output to be, if not the text you put in faultcode with the sprintf command?
gfvalvo
10
NiekBeijloos:
Does the println() function take in a pointer as parameter?
One of its overloaded versions does. Find Print.h in your Arduino installation. Inside you'll see:
size_t println(const char[]);
As has already been pointed out, the name of an array is treated as a pointer in function calls.
Edit:
So, the above is equivalent to:
size_t println(const char *);
NiekBeijloos:
I thought char*text is the definition of a String in C
The String class is C++, not C.
It has a capital S as a reminder and warning never to use it.