常数不恒定? [英] constants not constant?

查看:89
本文介绍了常数不恒定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这是非法的?


const int N = 10;

加倍[N];


我得到错误:在任何函数之外声明的可变大小类型。

这个语法规则背后的原因是什么?

How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any function".
What''s the reason behind this syntax rule?

推荐答案

ge*****@gmail.com 写道:< br>
为什么这是非法的?
const int N = 10;
double a [N];
我得到错误:在任何函数之外声明的可变大小类型。
这个语法规则背后的原因是什么?
How come this is illegal? const int N = 10;
double a[ N ]; I get "error: variable-size type declared outside of any function".
What''s the reason behind this syntax rule?




N是一个变量,其值在运行时设置。

因此在编译时无法知道''a'的大小。


-

克里斯。



N is a variable whose value is set at run-time.
Hence the size of ''a'' cannot be known at compile time.

--
Chris.


gene ... @ gmail.com写道:
gene...@gmail.com wrote:
为什么这是非法?

const int N = 10;
double a [N];

我得到错误:在任何功能这个语法规则背后的原因是什么?
How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any
function". What''s the reason behind this syntax rule?




在C中,const不代表常量,它意味着只读。


C99支持可变长度数组,但只能使用自动

(本地到功能)存储。


-

彼得



In C, const does not mean constant, it means read only.

C99 supports variable length arrays, but only with automatic
(local to function) storage.

--
Peter


< ge ***** @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
<ge*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
为什么这是非法的?

const int N = 10;
double a [N];

我得到错误:变量 - 在任何函数之外声明的大小类型"
这个语法规则背后的原因是什么?
How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any function".
What''s the reason behind this syntax rule?




const声明创建一个只读变量。如果你想要一个真正的

常数,在编译时知道它的值,那么这样做:


#define N 10


-

Daniel Rudy



A const declaration creates a read-only variable. If you want a true
constant who''s value is known at compile time, then do this:

#define N 10

--
Daniel Rudy


这篇关于常数不恒定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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