Hi everyone,
An old maths teacher once said to me "You don't have to understand it, you just have to be able to do it". Which is a surprisingly useful motto and has worked for me even through university ^_^
This question is sort of along those lines; in that I'm not really having any problems in this particular area with regards to my code not compiling/bugs in the program etc, but rather just interested in understanding why it must be done this particular way.
C++ doesn't like stuff like this
because when declaring an array the size must be constant and not a number not known until the program is run. However you can get around this by using something like thisCode:long aFunction(long n) { char anArray[n]; //some more code }
Now I don't really understand what the big difference here is. The line "anArray = new char[n]" is simply requesting a block of n pieces of memory each the size of a char. So why can it do this at runtime and not create an array in the first block of code?Code:long aFunction(long n) { char* anArray; anArray = new char[n]; //some more code delete[] anArray }
I'm still new to the language so I'm sure there must be something going over m head here, but if someone could explain it to me I would be most grateful
Kind Regards,
Stonehambey