为什么字符串文字L值,而所有其他文字是r值? [英] Why are string literals l-value while all other literals are r-value?

查看:105
本文介绍了为什么字符串文字L值,而所有其他文字是r值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 03 5.1主要EX pressions结果
§2:

文本是一个主要的前pression。它的类型取决于它的形式(2.13)上。文字字符串是一个左值;所有其他的文字是右值。

A literal is a primary expression. Its type depends on its form (2.13). A string literal is an lvalue; all other literals are rvalues.

这背后的原因是什么?结果
据我了解,字符串是对象,而所有其他的文字是not.And的左值总是指一个对象。

What is the rationale behind this?
As I understand, string literals are objects, while all other literals are not.And an l-value always refers to an object.

但随后的问题是,为什么是字符串对象,而所有其他文字都没有?结果
这个理由在我看来更像是一个鸡蛋或鸡的问题。

But the question then is why are string literals objects while all other literals are not?
This rationale seems to me more like an egg or chicken problem.

我理解这个问题的答案可能与硬件体系结构,而不是C / C ++为编程语言,不过我想听听相同。

I understand the answer to this may be related to hardware architecture rather than C/C++ as programming languages, nevertheless I would like to hear the same.

请注意:我标记了这个问题作为C&放大器; C ++一方面是因为C99标准也有类似的报价,具体的§6.5.1.4

Note: I am tagging this question as c & c++ both because C99 standard also has similar quotations, specifically §6.5.1.4

推荐答案

一个字符串是数组类型的文字,并用C没有办法一个数组类型在一个前pression存在,除了作为左值。本来是指定的字符串有指针类型(而不是通常衰减到一个指针数组类型)指向字符串内容,但这会让他们宁可少用;特别是的sizeof 运营商不能适用于他们。

A string literal is a literal with array type, and in C there is no way for an array type to exist in an expression except as an lvalue. String literals could have been specified to have pointer type (rather than array type that usually decays to a pointer) pointing to the string "contents", but this would make them rather less useful; in particular, the sizeof operator could not be applied to them.

需要注意的是C99引入了复合文字,这也是左值,所以有一个文字是左值不再是一个特殊的例外;它更接近于常态。

Note that C99 introduced compound literals, which are also lvalues, so having a literal be an lvalue is no longer a special exception; it's closer to being the norm.

这篇关于为什么字符串文字L值,而所有其他文字是r值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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