的C结构初始化可变 [英] C structure initialization with variable

查看:106
本文介绍了的C结构初始化可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到的,似乎没有任何的C类标准的C89保存为结构初始化的限制已经解除提后解决的问题。不过,我碰到的使用打开在Watcom IDE(调试)编译器在其中指出,初始化必须是常量前pression错误。

I've run into a problem that seems to not be addressed by any of the C Standards after C89 save for the mention that structures initialization limits had been lifted. However, I've run into an error using the Open Watcom IDE (for debugging) where the compiler states that the initializer must be a constant expression.

下面是发生了什么事情的要点。

Here's the gist of what's going on.

typedef struct{
 short x;
 short y;

} POINT;

void foo( short x, short y )
{
 POINT here = { x, y }; /* <-- This is generating the error for the compiler */

 /* ... */

}

什么标准的任何想法,为什么,或不允许的?

Any ideas why, or what standard disallows that?

推荐答案

下面引用距离的 C99理由

在C89委员会审议了关于允许自动
  总初始化到由一个大括号内的一系列的
  任意处决时间前pressions,而不是那些只可用于
  转换时静态初始化。而不是确定的一组
  规则,将避免病理情况下,但似乎并没有太
  随心所欲,C89委员会选举,只允许静
  初始化。这是重新考虑和执行时间前pressions是
  在C99中有效​​。

The C89 Committee considered proposals for permitting automatic aggregate initializers to consist of a brace-enclosed series of arbitrary execution-time expressions, instead of just those usable for a translation-time static initializer. Rather than determine a set of rules which would avoid pathological cases and yet not seem too arbitrary, the C89 Committee elected to permit only static initializers. This was reconsidered and execution-time expressions are valid in C99.

这篇关于的C结构初始化可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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