2016-09-23 86 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