为什么常量不能有逗号? [英] Why can't constants have commas?
问题描述
我的代码混淆器给了我这个:
char buff [1,9];
gcc反击:
" ISO C90禁止可变大小数组''buff''"
并检查标准,看来逗号确实被禁止
来自一个恒定的表达式。
为什么那样?只要
左右操作数也是常量表达式,允许使用逗号是不是更有意义?调用
表达式1,9可变大小很傻。
-Peter
My code obfuscator gave me this:
char buff[1, 9];
to which gcc retorted:
"ISO C90 forbids variable-size array ''buff''"
and checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.
Why''s that? Wouldn''t it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.
-Peter
推荐答案
Peter Ammon写道:
Peter Ammon wrote:
我的代码混淆器给了我这个:
char buff [1,9];
gcc反驳:
ISO C90禁止可变大小的数组''buff''"
并检查标准,看来逗号确实被禁止继续表达。
为什么?只要
左右操作数也是常量表达式,允许使用逗号是不是更有意义?调用
表达式1,9可变大小非常愚蠢。
My code obfuscator gave me this:
char buff[1, 9];
to which gcc retorted:
"ISO C90 forbids variable-size array ''buff''"
and checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.
Why''s that? Wouldn''t it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.
" 1,9"是C中的*表达式*,而不是常量。
如果您要做的是声明一个多维数组,那么
将是:
char buff [1] [9];
请查阅你所拥有的任何C书 - *和*(因为你应该在*发布之前已经完成了* b $ b已经完成*阅读常见问题解答。
http://www.eskimo.com/~scs/C-faq/top.html
HTH,
- $
-
Artie Gold - 德克萨斯州奥斯汀
如果你认为不重要,你就不会注意了。
"1, 9" is an *expression* in C, not a constant.
If what you''re trying to do is declare a multidimensional array, that
would be:
char buff[1][9];
Please look this up in whatever C book you have -- *and* (as you should
have done *before* posting) read the FAQ.
http://www.eskimo.com/~scs/C-faq/top.html
HTH,
--ag
--
Artie Gold -- Austin, Texas
"If you don''t think it matters, you''re not paying attention."
文章< news:cl *** *******@news.apple.com>
Peter Ammon< pe ********* @ rocketmail.com>写道:
In article <news:cl**********@news.apple.com>
Peter Ammon <pe*********@rocketmail.com> wrote:
...检查标准,看来逗号确实被禁止继续表达。
为什么?只要
左右操作数也是常量表达式,允许使用逗号是不是更有意义?调用
表达式1,9可变大小是非常愚蠢的。
... checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.
Why''s that? Wouldn''t it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.
要求标准委员会的意义可能会有点过分夸大b $ b。 :-)
严重的是,即使左操作数是*不*常数,
表达式本身仍然可以是常量。例如:
i =(printf(" hello \ n)"),2);
肯定会将i设置为2然而,在这种情况下,还有
a所需的副作用(写入hello \ n到stdout)。因此,
括号表达式显然不适合用作
静态初始化程序:
static int x =(printf (hello \ n),2); / *错误* /
但可能会被允许作为更一般的常量职位:
printf(我说);
{
int i,x [(printf(" ; hello \\\
"),2)];
for(i = 0; i< sizeof x / sizeof * x; i ++)
x [i] = printf(Hello hello \ n);
printf(我不知道你为什么说再见!);
printf( 我说你好了!;
}
所以也许,一旦开始走这条路,一个委员会小组
可能无法准确确定何时允许的内容。 :-)
-
现实生活:风河系统Chris Torek
美国犹他州盐湖城(40 °39.22''N,111°50.29''W)+1 801 277 2603
电子邮件:忘了它 http://web.torek.net/torek/index.html
阅读电子邮件就像在垃圾中搜索食物一样,感谢垃圾邮件发送者。
Asking for sense from the Standards Committees may be going a bit
overboard. :-)
Seriously, even if the left operand is *not* constant, the
expression itself could still be a constant. For instance:
i = (printf("hello\n"), 2);
is certain to set i to 2. In this case, however, there is also
a required side-effect (writing "hello\n" to stdout). As such,
the parenthesized expression is clearly unsuitable for use as a
static initializer:
static int x = (printf("hello\n"), 2); /* WRONG */
but might be allowed as a constant in "more general" positions:
printf("I say ");
{
int i, x[(printf("hello\n"), 2)];
for (i = 0; i < sizeof x / sizeof *x; i++)
x[i] = printf("Hello hello\n");
printf("I don''t know why you say goodbye\n");
printf("I say hello\n");
}
So perhaps, once one starts down this road, a committee subgroup
might not be able to agree as to precisely what is allowed when. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Peter Ammon写道:
Peter Ammon wrote:
我的代码混淆器给了我这个:
> char buff [1,9];
gcc反驳:
ISO C90禁止可变大小数组''buff''"
并检查标准,看来
逗号确实被禁止不断表达。
为什么?只要左右操作数也是常量表达式,允许使用逗号是否更有意义?
调用表达式1,9可变大小很傻。
cat buff.c
char buff [1,9];
gcc -Wall -std = c99 -pedantic -c buff.c
buff.c: 1:错误:在''之前解析错误,''令牌gcc --version
My code obfuscator gave me this:
char buff[1, 9];
to which gcc retorted:
"ISO C90 forbids variable-size array ''buff''"
and checking the standard, it appears that
commas are indeed forbidden from being in a constant expression.
Why''s that? Wouldn''t it make more sense to allow commas
as long as the left and right operands are also constant expressions?
Calling the expression 1,9 "variable-size" is pretty silly. cat buff.c char buff[1, 9];
gcc -Wall -std=c99 -pedantic -c buff.c buff.c:1: error: parse error before '','' token gcc --version
gcc(GCC)3.4.2
gcc (GCC) 3.4.2
这篇关于为什么常量不能有逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!