6
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

3
  • 3
    Are you compiling this as C, or as C++? They are not the same. Commented Aug 26, 2013 at 12:40
  • can't you use a std::vector ? it can be used to pass to legacy functions that receive arrays. Commented Aug 26, 2013 at 13:38
  • 1
    size should be a compile time constant... Commented May 25, 2015 at 15:44

5 Answers 5

5

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).
Sign up to request clarification or add additional context in comments.

2 Comments

Mike i'm using MFC in VC++
@ANjaNA: OK, I've removed the C tag to avoid confusion.
2

Normal use-case is to use new (and delete when you're done) for variable-sized elements. If you must use the stack, you can use alloca.

char *psz = new char[size+1];  // +1 you probably want zero-termination
...
delete [] psz;

3 Comments

The normal way is to use something like a std::vector.
@chris... yes, true as well... had my mind stuck in C there for a moment
2

you can use vector as a dynamic array. Try this.

#include <vector> // use this header
#include <iostream>

using namespace std; // use this namespace

int main()
{
    vector<int> v; //declare dynamic array equivalent

    for (int i = 0; i < 10; ++i) {
        v.push_back(10 + i);
    }

    cout << "vector data: " << endl;
    print_collection(v);

    while (v.begin() != v.end()) { 
        cout << "v.back(): "; print_elem(v.back()); cout << endl;
        v.pop_back();
    }
}

Comments

0

It produces an error because GetLength() returns you an arbitrary value, not statically defined.

The proper way is to allocate enough memory to hold your string and, if required, the NULL-terminated symbol either by calling malloc or by using the new operator (if compiling with C++ compiler).

Comments

0

I did something like this long time ago the Idea is to work with pointers.

so you need to make structure that has (char and nextPointer) the char represent the current value of the array and the nextPointer represent the next structure of the series

looping all through that pointers u will have your array of whatever you want your structure connected

struct yourStructure {
  Char char;
  double nextPointer;
} ;

so you create first structure and connect to the second and the tree

enter image description here

1 Comment

This is a linked list and there is an overhead of connecting each node. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.