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

How can PSTR receive multiple strings not separated by commas?

本文关键字:分隔 字符串 何接收 PSTR      更新时间:2023-10-16

我正在研究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 OKrn"
    "Content-Type: text/htmlrn"
    "Pragma: no-cachern"
    "rn"
    "<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 OKrn"
"Content-Type: text/htmlrn"

连接成一个字符串文字,相当于:

"HTTP/1.0 200 OKrnContent-Type: text/htmlrn"

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

编译器会自动将相邻的字符串文字连接为单个字符串文字。

printf("H" "e" "l" "l" "o");

相当于

printf("Hello");