如何创建VC可变大小字符数组++ [英] How to create a variable size char array in VC++

查看:191
本文介绍了如何创建VC可变大小字符数组++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  const int的SIZEA = 600;
烧焦SZ [SIZEA]

以上code正常工作。但低于code段导致错误。
我工作的Visual Studio 2005 - MFC应用程序

  CString的strFinal;....... // strFinal值动态变化。 。const int的大小= strFinal.GetLength();
烧焦SZ【规格】;


  

错误2错误C2057:预期不断前pression结果
  错误5错误C2070:'的char []':非法的sizeof操作结果
  错误4错误C2133:'深圳':未知大小错误3错误C2466:无法分配数组
  常量大小0



解决方案

在C ++的当前版本中,数组必须有固定的大小,编译时间常数指定。如果您需要使用运行时间值,那么你的选择是:


  • 最可移植的,使用动态数组类,如的std :: string的的std ::矢量<字符> ;

  • 使用支持C99变长数组作为非标准扩展编译器;

  • 等待一年动态数组(希望)在C ++ 14引入(也许等待的时间长一点你的编译器供应商追上)。

const int sizea = 600; 
char sz[sizea];

above code works fine. But below code segment cause errors. I'm working on visual studio 2005 - MFC application

CString strFinal;

.......//strFinal value is dynamically changing . . 

const int size = strFinal.GetLength();
char sz[size];

Error 2 error C2057: expected constant expression
Error 5 error C2070: 'char []': illegal sizeof operand
Error 4 error C2133: 'sz' : unknown size Error 3 error C2466: cannot allocate an array of constant size 0

解决方案

In the current version of C++, arrays must have a fixed size, specified by a compile-time constant. If you need to use a run-time value, then your options are:

  • most portably, use a dynamic array class such as std::string or std::vector<char>;
  • use a compiler that supports C99 variable-length arrays as a non-standard extension;
  • wait a year for dynamic arrays to (hopefully) be introduced in C++14 (and perhaps wait a bit longer for your compiler vendor to catch up).

这篇关于如何创建VC可变大小字符数组++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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