为什么VS要求数组大小为常数,而MinGW不需要?有没有解决的办法? [英] Why does VS require constant for array size, and MinGW does not? And is there a way around it?

查看:247
本文介绍了为什么VS要求数组大小为常数,而MinGW不需要?有没有解决的办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将我使用code :: blocks编写的Mingw的一些代码移植到Visual Studio及其编译器中,它收到了很多错误,我的数组大小必须恒定!为什么VS需要恒定的大小而mingw不需要?

I have ported some code from Mingw which i wrote using code::blocks, to visual studio and their compiler, it has picked up many errors that my array sizes must be constant! Why does VS need a constant size and mingw does not?

例如

const int len = (strlen(szPath)-20);
char szModiPath[len];

len变量用红色下划线表示它是一个错误并表示期望的常量表达式"

the len variable is underlined in red to say that its an error and says "expected constant expression"

我想解决这个问题的唯一方法是....

The only way i can think to get around this is....

char* szModiPath = new char[len];
delete[] szModiPath;

我是否必须将所有内容更改为动态,否则VS中还有另一种方法吗?

Will i have to change everything to dynamic or is there another way in VS?

推荐答案

为什么VS需要恒定的大小而mingw不需要?

Why does VS need a constant size and mingw does not?

因为可变长度数组不是C ++的一部分,尽管MinGW(g ++)支持它们作为扩展. 数组大小必须是C ++中的常量表达式.

Because Variable Length Arrays are not a part of C++ although MinGW(g++) supports them as extension. Array size has to be a constant expression in C++.

在C ++中,始终建议使用std::vector而不是C-style arrays. :)

In C++ it is always recommended to use std::vector instead of C-style arrays. :)

这篇关于为什么VS要求数组大小为常数,而MinGW不需要?有没有解决的办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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