2016-09-23 35 views
-2

目前我在Arduino上编写C++代码。在示例中,我发现的,而不是直接使用字符串表达式C(字符串)在C++中的含义是什么?

Serial.print(F("Disconnected from central:")); 

很明显,这种说法是用来发送字符串到串行的,但为什么它使用F(串)?

我尝试谷歌它,但没有结果。如果有人知道它,我将不胜感激。

+3

有一个称为'F'的函数或一个具有单个参数的宏'F'。 – dasblinkenlight

+4

https://www.baldengineer.com/arduino-f-macro.html – interjay

回答

2

这个宏是Arduino特有的,它不是“C++”。

它将字符串放在闪存中,以节省RAM。

这意味着程序运行时不能修改字符串。

当前的一个定义是:

#define F(slit) (reinterpret_cast<const __FlashStringHelper *>(PSTR(slit))) 

See the source code for more

+0

这是一个有点偏离主题,但我不禁想知道为什么他们不只是默认将字符串文字放在闪存中。默认情况下将它们放入RAM有什么好处吗? –

+0

@IanAbbott I beleive [this page](https://www.iar.com/support/tech-notes/compiler/strings-with-iccavr-2.x/)有一些讨论,当你有指向AVR平台上的字符串。 – unwind

+0

所以我明白了!感谢那。 –

0

F()是最强大的功能之一,1.0IDE的发行版中增加了功能。我一直在混合条款macrofunctionF()真不是function,它是一个#define macro它生活在WString.h

#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal))) 

这是一个宏,它使用在flash memory而不是RAM存储strings