在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/C  C++/ C++ 字符串加字符

C++ 字符串加字符

cout << ("s" + 'a');

這樣一段代碼,為什么輸出是一個空行呢?
難道是 string literal + character 沒有這個運算嗎?
沒有的話為什么輸出是空行呢?

C++ 中, string literal 就是 const char[n] 嗎?

回答
編輯回答
別逞強(qiáng)

表達(dá)式"s"的類型是const char[],表達(dá)式 'a'的類型是char。

數(shù)組是不能進(jìn)行內(nèi)置加法運算的。所以const char []會被轉(zhuǎn)換成const char *,這里的運算就變成了"指針+整型"(char是一種整型)。輸出空行的運行結(jié)果實際上是數(shù)組越界引起的。

String literal
Narrow multibyte string literal. The type of an unprefixed string literal is const char[]

Additive operators
addition. For the built-in operator, lhs and rhs must be one of the following: Both have arithmetic or unscoped enumeration type. In this case, the usual arithmetic conversions are performed on both operands and determine the type of the result. One is a pointer to complete object type, the other has integral or unscoped enumeration type. In this case, the result type has the type of the pointer

Array-to-pointer decay
There is an implicit conversion from lvalues and rvalues of array type to rvalues of pointer type: it constructs a pointer to the first element of an array. This conversion is used whenever arrays appear in context where arrays are not expected, but pointers are.

2017年11月19日 10:15