c++ - How to create a variable size char array in VC++ -
const int sizea = 600; char sz[sizea];
above code works fine. below code segment cause errors. i'm working on visual studio 2005 - mfc application
cstring strfinal; .......//strfinal value 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 array of constant size 0
in current version of c++, arrays must have fixed size, specified compile-time constant. if need use run-time value, options are:
- most portably, use dynamic array class such
std::string
orstd::vector<char>
; - use compiler supports c99 variable-length arrays non-standard extension;
- wait year dynamic arrays (hopefully) introduced in c++14 (and perhaps wait bit longer compiler vendor catch up).
Comments
Post a Comment