如何在其他代码之后的Visual Studio C89中声明可变长度数组 [英] How to declare a variable length array in Visual Studio C89 following other code

查看:238
本文介绍了如何在其他代码之后的Visual Studio C89中声明可变长度数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在VS中所有变量必须在块的顶部声明,但是如果我要VLA,即.如果我想做这样的事情:

I understand in VS all variables must be declared at the top of a block, but if I want a VLA, ie. if I wanted to do something like this:

int result = runalgorithm(); 

int vla[result];

上面的代码无效,因为必须在顶部声明vla.除了创建任意大数组之外,还有什么好的解决方案?

the code above is invalid, because vla must be declared at the top. What is a good solution for this, other than creating an arbitrarily large array?

推荐答案

您不能. VLA C99和更高版本的标准中受支持. (在C99中支持是强制性的;在C11中是可选的.)C89没有VLA概念或对此不提供支持.

You cannot. VLA is supported in C99 and later standards. (Support is mandatory in C99; it is optional in C11.) C89 does not have the VLA concept or support for it.

您可以选择动态内存分配.看看 malloc() 及其家族.

You can choose dynamic memory allocation, instead. have a look at malloc() and family for your reference.

请记住,如果要使用动态内存(分配),则必须 free() 使用完分配的内存后.

Remember, if you want to use dynamic memory (allocation), you have to free() the allocated memory once you're done using it.

这篇关于如何在其他代码之后的Visual Studio C89中声明可变长度数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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