简体   繁体   English

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

[英]How can PSTR receive multiple strings not separated by commas?

I'm studying the code from the rbb_server example of ENC28J60's library for Arduino (I would put the link here if I could) and I've noticed this wierd piece of code: 我正在研究ENC28J60用于Arduino的库的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();
}

How can that PSTR(........) compile if strings dont even have a comma separating them?? 如果字符串甚至没有逗号分隔它们,该PSTR(........)怎么编译?

I've looked for that PSTR macro definition and I've found this: 我一直在寻找该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 */

Which you can find in the pgmspace.h file somewhere in Arduino's IDE folder. 您可以在Arduino IDE文件夹中的pgmspace.h文件中找到该文件。

How can this even compile?? 这怎么编译?

Thanks! 谢谢!

There is a rule in C (C99, 6.4.5p4) that says that two adjacent string literals (be they on different lines): 在C(C99,6.4.5p4)中有一条规则说两个相邻的字符串文字(它们在不同的行上):

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

are concatenated into one string literal and are equivalent to: 连接到一个字符串文字中,等效于:

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

This is done by the preprocessor in translation phase 6. 这由预处理器在翻译阶段6中完成。

Adjacent string literals are automatically concatenated by the compiler into a single string literal. 相邻的字符串文字会由编译器自动连接为单个字符串文字。

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

is equivalent to 相当于

printf("Hello");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM