标准转换:数组到指针转换(字符串) [英] standard conversions: Array-to-pointer conversion (strings)

查看:161
本文介绍了标准转换:数组到指针转换(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  (2.13.4)不是一个宽字符串字面量可以转换
到类型指向字符的右值;一个宽字符串文字可以
转换为类型指向wchar_t的指针的右值。在任何一种情况下,
的结果都是指向数组的第一个元素的指针。这种转换
只有当有一个明确的适当的指针目标
类型,而不是当一般需要从一个左值转换为
一个右值。 [注意:此转换已弃用。 ]

为了在过载分辨率(13.3.3.1.1)中排名,这个
转换被认为是一个数组到指针的转换,然后是一个
限定转换4.4)。

[示例:abc被转换为指向const char的指针,作为数组到指针
的转换,然后转换为指向char的指针作为限定转换。

任何人都可以用示例程序解释这一点。



我知道关于字符串字面量...可能我知道上面的语句(宽字符串字面量前缀L用法)。我知道..宽字符串文字意思。

解决方案

之前 const 被引入C,许多人写这样的代码:

  char * p =hello world ; 

因为写入字符串文字是未定义行为,但此危险转换已弃用,但由于语言更改不应破坏现有代码,因此此转换不会立即废弃。



使用指向常量字符的指针是合法的,因为const正确性不允许你通过它写:

  const char * p =hello world; 

这就是真的。如果您需要更多信息,请提出具体问题。


This is the point from ISO :Standard Conversions:Array-to-pointer conversion: $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. ]

Can any one explain this, if possible with an example program.

I thing i know regarding string literals...may i know about above statement(wide string literal Prefix L usage).I know ..about the wide string literal meanig.But i need it according to the above satement I mean with Lvaue to Rvalue Conversions.

解决方案

Before const was introduced into C, many people wrote code like this:

char* p = "hello world";

Since writing to a string literal is undefined behavior, this dangerous conversion was deprecated. But since language changes shouldn't break existing code, this conversion wasn't deprecated immediately.

Using a pointer to a constant character is legal, since const correctness does not let you write through it:

const char* p = "hello world";

And that's all there really is too it. Ask specific questions if you need more information.

这篇关于标准转换:数组到指针转换(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆