int数组[N];当N不是常数时 [英] int array[N]; when N is not a constant

查看:326
本文介绍了int数组[N];当N不是常数时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是否合法?


void fun(int N){

int arr [N];

}


gcc和Digital Mars接受它,但msvc抱怨它想要一个

常量表达式。

Is the following legal?

void fun(int N) {
int arr[N];
}

gcc and Digital Mars accept it, but msvc complains that it wants a
constant expression.

推荐答案

Szabolcs< sz ****** @ gmail.com在新闻中写道:fb *********** @ toralf.uib.no:
Szabolcs <sz******@gmail.comwrote in news:fb***********@toralf.uib.no:

以下是否合法?


void fun(int N){

int arr [N];

}


gcc和Digital Mars接受它,但msvc抱怨它想要一个

常量表达式。
Is the following legal?

void fun(int N) {
int arr[N];
}

gcc and Digital Mars accept it, but msvc complains that it wants a
constant expression.



这必须是这些编译器的扩展。数组需要一个

积分常量表达式[dcl.array],它在[expr.const]

中定义为:


一个整数常量表达式只能包含文字(2.13),

枚举器,const变量或整数的静态数据成员或用常量表达式初始化的
枚举类型(8.5) ,nontype

整数或枚举类型的模板参数,以及sizeof

表达式。浮动文字(2.13.3)只有在被转换为

整数或枚举类型时才会出现。


希望有所帮助。


joe

This would have to be an extension for these compilers. Arrays require a
integral constant expression [dcl.array] which is defined in [expr.const]
as:

"An integral constant expression can involve only literals (2.13),
enumerators, const variables or static data members of integral or
enumeration types initialized with constant expressions (8.5), nontype
template parameters of integral or enumeration types, and sizeof
expressions. Floating literals (2.13.3) can appear only if they are cast to
integral or enumeration types."

Hope that helps.

joe


Szabolcs写道:
Szabolcs wrote:

以下是否合法?


void fun(int N){

int arr [N];

}


gcc和Digital Mars接受它,但msvc抱怨它想要一个

常量表达式。
Is the following legal?

void fun(int N) {
int arr[N];
}

gcc and Digital Mars accept it, but msvc complains that it wants a
constant expression.



这是一个C99主义。可变长度数组(VLA)在C99中是合法的,而不是在
C90或C ++ 98或C ++ 03中。


有一个gcc的选项,指定接受哪些方言。我不知道它是什么,你可以在gnu.g ++中提出帮助。


再说一次,总的来说,它不是在C ++中合法。

It''s a C99-ism. Variable length arrays (VLAs) are legal in C99, not in
C90 or C++98 or C++03.

There''s an option to gcc which specifies which dialects to accept. I
don''t remember what it is, you can ask in gnu.g++.help.

Again, in summary, it''s not legal in C++.


在2007-09-06 22:46,red floyd写道:
On 2007-09-06 22:46, red floyd wrote:

Szabolcs写道:
Szabolcs wrote:

>以下是否合法?

void fun(int N){
int arr [N];
}

gcc和Digital Mars接受它,但msvc抱怨它需要一个
常量表达式。
>Is the following legal?

void fun(int N) {
int arr[N];
}

gcc and Digital Mars accept it, but msvc complains that it wants a
constant expression.



这是一个C99主义。可变长度数组(VLA)在C99中是合法的,而不是在
C90或C ++ 98或C ++ 03中。


有一个gcc的选项,指定接受哪些方言。我不知道它是什么,你可以在gnu.g ++。帮助中询问。


It''s a C99-ism. Variable length arrays (VLAs) are legal in C99, not in
C90 or C++98 or C++03.

There''s an option to gcc which specifies which dialects to accept. I
don''t remember what it is, you can ask in gnu.g++.help.



-std = c ++ 98,其他不错的选择是-pedantic和-Wall


-

Erik Wikstr?m

-std=c++98, other good options are -pedantic and -Wall

--
Erik Wikstr?m


这篇关于int数组[N];当N不是常数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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