问题描述
在C99中,我们可以将复合文字用作未命名的数组.
In C99 we can use compound literals as unnamed array.
但这是文字常量,例如100
,'c'
,123.4f
等.
But are this literals constants like for example 100
, 'c'
, 123.4f
, etc.
我注意到我可以做到:
((int []) {1,2,3})[0] = 100;
而且,我没有编译错误,并且可以猜测该未命名数组的第一个元素被修改为100.
and, I have no compilation error and is guessable that the first element of that unnamed array is modified with 100.
因此,与复合文字一样,数组似乎是左值而不是常量值.
So it seems as array as compound literal are lvalue and not constant value.
推荐答案
这是一个左值,如果我们查看草稿C99标准部分6.5.2.5
复合文字表示(强调我的 ):
It is an lvalue, we can see this if we look at the draft C99 standard section 6.5.2.5
Compound literals it says (emphasis mine):
如果要使用 const 版本,则稍后在同一部分中将给出以下示例:
If you want a const version, later on in the same section it gives the following example:
(const float []){1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6}
我们可以在Dobb博士的文章 The新增C:复合文字,并说:
We can find an explanation of the terminology in this Dr Dobb's article The New C: Compound Literals and says:
这篇关于数组作为复合文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!