gpt4 book ai didi

c++ - PSTR如何接收多个不以逗号分隔的字符串?

转载 作者:行者123 更新时间:2023-11-28 07:32:06 25 4
gpt4 key购买 nike

我正在研究 Arduino 的 ENC28J60 库的 rbb_server 示例中的代码(如果可以的话,我会把链接放在这里),我注意到这段奇怪的代码:

 static word homePage() {
long t = millis() / 1000;
word h = t / 3600;
byte m = (t / 60) % 60;
byte s = t % 60;
bfill = ether.tcpOffset();
bfill.emit_p(PSTR(
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n"
"\r\n"
"<meta http-equiv='refresh' content='1'/>"
"<title>RBBB server</title>"
"<h1>$D$D:$D$D:$D$D</h1>"),
h/10, h%10, m/10, m%10, s/10, s%10);
return bfill.position();
}

如果字符串连逗号分隔都没有,那么 PSTR(........) 怎么能编译呢??

我查找了那个 PSTR 宏定义并找到了这个:

Used to declare a static pointer to a string in program space. */
# define PSTR(s) ((const PROGMEM char *)(s))
#else /* !DOXYGEN */
/* The real thing. */
# define PSTR(s) (__extension__({static char __c[] PROGMEM = (s); &__c[0];}))
#endif /* DOXYGEN */

您可以在 Arduino 的 IDE 文件夹中某处的 pgmspace.h 文件中找到它。

这怎么能编译??

谢谢!

最佳答案

C (C99, 6.4.5p4) 中有一条规则说两个相邻的字符串文字(如果它们在不同的行):

"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"

连接成一个字符串文字,等同于:

"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n"

这是由翻译阶段 6 中的预处理器完成的。

关于c++ - PSTR如何接收多个不以逗号分隔的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17453176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com