Need funcion to return char[]?
I mentioned this in another thread, and this is a common tripping point, but an array isn't really a variable, its just a trick to look like one. At the end of the day, an array is just syntactic sugar over a continuous block of memory.
That's incorrect. An array is a type that's distinct from the concept of a continuous block of memory. In particular, dynamic arrays returned by new or malloc (and other functions) represent continuous blocks of memory, but are not of array type. Arrays are just a somewhat inconsistant feature of C/C++. It's not sugar.
But it's true that arrays behave differently from other variable types.
Exactly what distinction exists about arrays that does not amount to syntactic sugar? I can use array subscript notation to navigate through newed/malloced memory, and I can use pointer arith to navigate static/stack based arrays. As far as I'm aware, all of the inconsistencies of array notation in C\C++ are a result of the fact that, under the covers, arrays are just pointers to a block of data, however that data is allocated, and array subscript notation is just another form of dereferencing that pointer.
As SiCrane illistrated, arrays are not just poiners to dynamic memory. They are instead a block of memory inline with the other variables in the scope they are declared, be that the stack, inside a class, or as global. This is most apparent in the disharmony between 2D arrays, and what would be the dynamic equivilent, an array of pointers to dynamic arrays.
This:
int array1[10][10] = {{}};
array1[4][6]=1;
Creates a continious block of memmory typically of 400 bytes. The indexing is equivelent to somthing like (int*)(int(array1) + 10 * 4 + 6). Also, as pointed out, the base pointer itself does not live in memory.
This:
int **array2 = new int *[10];
for(int i=0;i<10;++i)
array2 = new int[10];
array1[4][6]=1;
Creates an array of pointers to array. It uses a total of 440 bytes, on 32 bit processors, typically. The index calculation is trivial: *(array2+4)+6, but it does two extra memory loads.
(Note: I have not tested the code, and it may contain typos or errors)