包含多个字符的字符常量
A character constant with more then one character
我正在检查我在stackoverflow上看到的一些答案,并以一种根据非常有经验的程序员不应该工作的方式更改了一行,令人惊讶的是它做到了。有人能解释一下为什么这是可能的吗?问题是一个字符常量与多个字符(我使用Visual Studio 2013)
// stack.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using std::cout;
int * foo()
{
int a = 5;
return &a;
}
int main()
{
int* p = foo();
cout << *p << ' '; // this line should not compile but it did???
*p = 8;
cout << *p << 'n';
}
有人能解释一下为什么这是可能的吗?
因为语言允许这样的事情;它被称为多字符文字。用c++ 11 2.14.3/1的话说:
多字符文字类型为
int
,并且是由实现定义的价值。
通常,每个字符(足够短的字面值)将映射到int
值的一个字节,因此'ab'
和'ba'
应该具有不同的值;要了解详细信息,请参考编译器的文档。
相关文章:
- 如何循环访问常量字符**?
- constexpr 函数获取常量字符*
- 如何计算常量字符**中的总字符数?
- QDial:如何将 int 值传递给需要常量字符*的 Qfile?
- 为什么 std::string_view 比常量字符*快?
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 在编译时将常量字符* 转换为常量 char_type*
- 将字符串数组传递给接受常量字符**的函数
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 字符串强制转换为常量字符*
- 使用常量字符*时退出代码 139
- 为什么我可以隐式地将字符*转换为常量字符*,但不能将无符号字符*
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- 无法<string>从"常量字符 []"转换为<类名>
- 初始化常量字符* 数组
- 常量字符*的性质是什么?
- C++ 获取向量中常量字符* [ ] 的长度
- 无法将参数 1 从 WCHAR 转换为常量字符 *
- 如何在 c++ 中将向量转换为<string>常量字符* const*?
- C++将常量字符* 指针数组传递给对象