Array Sizing issue

For the record I feel stupid posting this!
why can't I do the following

int a = myfunction.Count(); //obviously a function that returns an int
int myArrayAngel;

I cant even do this

int a = 5;
int myArray [5];

Is there something wrong with my syntax or is this not possible in c++

Thanks


Will


Answer this question

Array Sizing issue

  • Adhisuma

    Because C++ doesn't support it. The idea is that the stack size can be calculated during compile time. You are dynamicaly sizing an item on the stak.

    So either use new/delete, or check if you can use alloca

  • Phil Pownall

    C++ does not support dynamic sizing of static arrays (Which is why they are called static arrays). You could either use a dynamic array (Using the new operator and pointers):


    int a = myfunction.Count();
    int* myArray = new int[ a ];

     


    Or you could bypass arrays altogether and go with the STL:



    #include <vector>

    int a = myfunction.Count();
    vector<int> myVector(a);

     


    I'm pretty sure both of these options compile and work... Alas, I could be wrong, but still, try 'em out.


  • Array Sizing issue