数组编译时出错error C2057: expected constant expression,以及解决办法

本文探讨了在C++中如何解决数组大小由运行时变量决定导致的编译错误,并提供了三种有效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>

using namespace std;

void main()

{

    int i=10;

   int a[i];

}

编译时提示错误:error C2057: expected constant expression

原因:i是个变量,一直要到运行期才被分配内存,才会有值,所以编译期的时候它还没有值,故而编译时会出错。


解决办法一:采用#define标示符常量


 #include <iostream>

using namespace std;
#define  i  10
void main()
{
   
   int a[i];
}

解决办法二:采用const只读变量当作全局变量

#include <iostream>
using namespace std;
const int i=100;

void main()
{
   
   int a[i];
}


解决办法三:采用enum枚举型变量


#include <iostream>
using namespace std;


enum {i=100 ,j=200};

void main()
{
   
   int a[i];
}

   为什么这三种办法可行?

   因为这三种形式使i都在编译期便被赋值了。因为这几种形式的量不会占用对象的存储空间,所以它们在编译时可以被全部求值(如果需要占用空间,则在编译时是不可以被赋值的,因为编译期是不分配内存的)


  全局变量与静态变量需要占用存储空间,在编译时地址被确定好,至于内存的分配还要等到运行期,所以把i当作全局变量或者是静态变量,数组编译时依旧会出错。






config.h: 12: (374) missing basic type; int assumed (warning) config.h: 12: (372) "," expected (error) config.h: 12: (192) undefined identifier "P3" (error) config.h: 12: (188) constant expression required (error) config.h: 13: (374) missing basic type; int assumed (warning) config.h: 13: (372) "," expected (error) config.h: 13: (192) undefined identifier "P1" (error) config.h: 13: (188) constant expression required (error) config.h: 14: (374) missing basic type; int assumed (warning) config.h: 14: (372) "," expected (error) config.h: 14: (188) constant expression required (error) config.h: 15: (374) missing basic type; int assumed (warning) config.h: 15: (372) "," expected (error) config.h: 15: (192) undefined identifier "P0" (error) config.h: 15: (188) constant expression required (error) config.h: 12: (374) missing basic type; int assumed (warning) config.h: 12: (372) "," expected (error) config.h: 12: (192) undefined identifier "P3" (error) config.h: 12: (188) constant expression required (error) config.h: 13: (374) missing basic type; int assumed (warning) config.h: 13: (372) "," expected (error) config.h: 13: (192) undefined identifier "P1" (error) config.h: 13: (188) constant expression required (error) config.h: 14: (374) missing basic type; int assumed (warning) config.h: 14: (372) "," expected (error) config.h: 14: (188) constant expression required (error) config.h: 15: (374) missing basic type; int assumed (warning) config.h: 15: (372) "," expected (error) config.h: 15: (192) undefined identifier "P0" (error) config.h: 15: (188) constant expression required (error) main.c: 51: (192) undefined identifier "TMR0H" (error) main.c: 52: (192) undefined identifier "TMR0L" (error) config.h: 12: (374) missing basic type; int assumed (warning) config.h: 12: (372) "," expected (error) config.h: 12: (192) undefined identifier "P3" (error) config.h: 12: (188) constant expression required (error) config.h: 13: (374) missing basic type; int assumed (warning) config.h: 13: (372) "," expected (error) config.h: 13: (192) undefined identifier "P1" (error) config.h: 13: (188) constant expression required (error) config.h: 14: (374) missing basic type; int assumed (warning) config.h: 14: (372) "," expected (error) config.h: 14: (188) constant expression required (error) config.h: 15: (374) missing basic type; int assumed (warning) config.h: 15: (372) "," expected (error) config.h: 15: (192) undefined identifier "P0" (error) config.h: 15: (188) constant expression required (error) timer.c: 5: (192) undefined identifier "TCON0" (error) timer.c: 7: (192) undefined identifier "TMR0H" (error) timer.c: 8: (192) undefined identifier "TMR0L" (error) timer.c: 10: (192) undefined identifier "TMR0_IE" (error) timer.c: 11: (192) undefined identifier "TMR0_RUN" (error) timer.c: 12: (192) undefined identifier "EA" (error) timer.c: 17: (192) undefined identifier "PWMCON1" (error) timer.c: 21: (192) undefined identifier "PWMCON0" (error) timer.c: 24: (192) undefined identifier "PWMPH" (error) timer.c: 25: too many errors (21)
最新发布
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值