0

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:

 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??

I've looked for that PSTR macro definition and I've found this:

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.

How can this even compile??

Thanks!

4

2 回答 2

1

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 中的预处理器完成的。

于 2013-07-03T16:20:03.550 回答
0

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

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

相当于

printf("Hello");
于 2013-07-03T16:21:20.890 回答