What is wrong here.I want to set the array myarray depending on the value of the variable e2 and not on some specific number. Why doesn't this work.
Code:int e2 = 15;
char myarray[(int)e2];
Printable View
What is wrong here.I want to set the array myarray depending on the value of the variable e2 and not on some specific number. Why doesn't this work.
Code:int e2 = 15;
char myarray[(int)e2];
You can't allocate an array like that. You're allocating it on the stack, so you have to give a constant size. If you want to give it a dynamic size, you have to allocate it on the heap:
...although don't try to use myarray after it's been deleted.Code:int e2 = 15;
char *myarray = new char[e2];
// ...use myarray
delete myarray; // Very important!!!
Thanks for the code it works just fine