声明大小为整数变量的数组时,为什么会出现编译器错误? [英] Why is there a compiler error, when declaring an array with size as integer variable?

查看:85
本文介绍了声明大小为整数变量的数组时,为什么会出现编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio中,我遇到了Dev-C ++中以前没有的错误:

In visual studio, I have an error that I didn't have before in Dev-C++:

int project = (rand() % 5) + 1 ;
int P[project][3];

编译:


错误C2057:期望的常量表达式

错误C2466:无法分配常量大小为0的数组

错误C2133: P:未知大小

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'P' : unknown size

您能帮助您理解此错误吗?

Can you help to understand this error?

推荐答案

在这种情况下,您需要动态分配内存。因此,您不能说 int P [someVariable] 。您需要使用 int * mem = new int [someVariable]

You need to allocate memory dynamically in this case. So you cannot say int P[someVariable]. You need to use int *mem = new int[someVariable]

看看此链接。

这篇关于声明大小为整数变量的数组时,为什么会出现编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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