进一步熟悉nullptr
#ifndef emptyString#define emptyString F("")
#endif
void foo(const char *payload = nullptr) {
if (payload == nullptr) {
Serial.println("emptyString");
} else {
Serial.print("payload: ");
Serial.println(payload);
}
}
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
}
/*
* Main function, get and show the temperature
*/
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
foo();
foo("request to all devices on the bus");
delay(1000);
}
程序写入ESP32C3以后,串口吐的内容是
emptyString
payload: request to all devices on the bus
emptyString
payload: request to all devices on the bus
emptyString
payload: request to all devices on the bus
#define emptyString F("")
定义空字符串
foo(const char *payload = nullptr)
定义入参payload,默认值是nullptr
foo();
让入参使用默认值,打印emptyString
foo("request to all devices on the bus");
打印payload: request to all devices on the bus
对语法记不清,最好是写出来验证一下
#ifndef emptyString
#define emptyString F("")
#endif
String foo(const char *payload = nullptr) {
if (payload == nullptr) {
return emptyString;
Serial.println("emptyString");
}
Serial.print("payload: ");
Serial.println(payload);
return "foo";
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Serial begin...");
foo();
foo("Serial Test...");
}
void loop() {
// put your main code here, to run repeatedly:
}
有兴趣可以运行下这个代码,有没有什么问题呢?
页:
[1]