Why we can do something like
for(int i = 0; i < destination->imageSize; i=i+3)
{
buffer[2] = destination->imageData[i];
buffer[1] = destination->imageData[i+1];
buffer[0] = destination->imageData[i+2];
buffer+=3;
}
but we can not do
char buffer[destination->imageSize];
And how to such thing?
Sorry – I am quite new to C++…
BTW: my point is to create a function that would return a char with an image. If I’d use mem copy how do I delete returned value?
I have to ask, why do you think they’re at all related? If you couldn’t index an array by runtime variable, it’d be pretty useless for there to even be arrays. Declaring a variable of a size governed by a runtime variable is entirely different and requires fundamental changes to the way the compiler manages automatic memory.
Which is why you can’t do it in C++. This may change, but for now you can’t.
If you really need a variable sized array you need to allocate one dynamically. You can do it the hard, f’d up way (
char * buff = new char[size]...delete [] buff;), or you can do it the easy, safer way (std::vector<char> buff(size)). Your choice. You can’t build it “on the stack” though.