Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 标准转换:数组到指针的转换(字符串)_C++ - Fatal编程技术网

C++ 标准转换:数组到指针的转换(字符串)

C++ 标准转换:数组到指针的转换(字符串),c++,C++,这是ISO:标准转换:数组到指针转换:$4.2.2中的点 A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either

这是ISO:标准转换:数组到指针转换:$4.2.2中的点

   A string literal (2.13.4) that is not a wide string literal can be converted 
    to an rvalue of type “pointer to char”; a wide string literal can be 
    converted to an rvalue of type “pointer to wchar_t”. In either case,
    the result is a pointer to the first element of the array. This conversion 
    is considered only when there is an explicit appropriate pointer target 
    type , and not when there is a general need to convert from an lvalue to
    an rvalue. [Note: this conversion is deprecated. ] 

   For the purpose of ranking in overload resolution (13.3.3.1.1), this 
   conversion is considered an array-to-pointer conversion followed by a
   qualification conversion (4.4). 

  [Example:"abc" is converted to "pointer to const char” as an array-to-pointer  
  conversion, and then to  “pointer to char” as a qualification conversion. ]
如果可能的话,任何人都可以用一个示例程序来解释这一点


关于字符串文字,我知道一件事…我可以知道上面的语句吗(宽字符串文字前缀L用法)。我知道..关于宽字符串文字的意思。但是根据上面的语句,我需要它,我的意思是Lvaue到右值的转换。

常量
被引入C之前,许多人编写的代码如下:

char* p = "hello world";
由于写入字符串文字是不正确的,因此不推荐使用这种危险的转换。但由于语言更改不应破坏现有代码,因此这种转换并没有立即遭到反对

使用指向常量字符的指针是合法的,因为常量正确性不允许您写入:

const char* p = "hello world";

就这些,真的太多了。如果你需要更多信息,可以问一些具体的问题。

在C语言引入const之前,很多人编写的代码如下:

char* p = "hello world";
由于写入字符串文字是不正确的,因此不推荐使用这种危险的转换。但由于语言更改不应破坏现有代码,因此这种转换并没有立即遭到反对

使用指向常量字符的指针是合法的,因为常量正确性不允许您写入:

const char* p = "hello world";
就这些,真的太多了。如果您需要更多信息,请提出具体问题。

写作时

cout<<*str    //output :s
写入时将打印“t”

cout<<*str    //output :s
   char* str = "stackoverflow";
   cout << str;  //output :stackoverflow
将打印“t”

   char* str = "stackoverflow";
   cout << str;  //output :stackoverflow
在这里,
*str
的类型就是char——使用前导的
*
取消对指针的引用,并返回“指向的对象”,这只是一个字符


这里,
*str
的类型只是char——使用前导的
*
取消对指针的引用,并返回“指向的对象”,这只是一个字符。

实际上,我的疑问是关于宽字符串文字。我的意思是你必须使用L前缀。我需要程序扩展。它告诉了上面的点/语句。在C++0x中,转换将被完全删除,并将拒绝断开的代码。所以ppl最好现在就改变他们的坏代码。实际上,我的疑问是关于宽字符串文字。我的意思是你必须使用L前缀。我需要程序的扩展。它告诉上面的点/语句。在C++0x中,转换将被完全删除,并将拒绝坏代码。所以ppl最好现在就修改他们的坏代码。