I want to get the size of an array from return value of a function. This is the function:
const char* bukaKunci(){
if(mlx.readObjectTempC() >= tempMin){ // if temp is lower than minimum temp
digitalWrite(selenoid, HIGH);
delay(1000);
return "Kunci terbuka!";
}
else{
digitalWrite(selenoid, LOW);
delay(1000);
return "Pintu terkunci";
}
return 0;
}
But when I check the size with this line:
const char* msg = bukaKunci();
int msg_len = sizeof(msg);
Serial.println(msg);
Serial.println(msg_len);
It gives me output the size of msg is 2, like this:
Kunci terbuka!
2
Did I missing something?
sizeoftells you the size of the pointer here, not the size of the array whose first element the pointer points to. In general, you cannot determine the size of an array given only a pointer to the first element. However, since you know the pointer always points to a null-terminated string here, you can usestrlento find the length of the string.sizeofis applied to a pointer, it gives the size of the pointer, regardless of whether that pointer points at (the first element of) an array.