进一步熟悉nullptr

2024-04-282426
  1. #ifndef emptyString
  2. #define emptyString F("")
  3. #endif
  4. void foo(const char *payload = nullptr) {
  5.   if (payload == nullptr) {
  6.     Serial.println("emptyString");
  7.   } else {
  8.     Serial.print("payload: ");
  9.     Serial.println(payload);
  10.   }
  11. }
  12. void setup(void)
  13. {
  14.   // start serial port
  15.   Serial.begin(9600);
  16.   Serial.println("Dallas Temperature IC Control Library Demo");
  17. }
  18. /*
  19. * Main function, get and show the temperature
  20. */
  21. void loop(void)
  22. {
  23.   // call sensors.requestTemperatures() to issue a global temperature
  24.   foo();
  25.   foo("request to all devices on the bus");
  26.   delay(1000);
  27. }
复制代码

程序写入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


对语法记不清,最好是写出来验证一下



创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(1)
小牛哥
小牛哥
作者
[code]#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:
}[/code]
有兴趣可以运行下这个代码,有没有什么问题呢?
- 没有更多了 -