C++ Initialize array pointer

You can't initialize a dynamically allocated array that way. Neither you can assign to an array(dynamic or static) in that manner. That syntax is only valid when you initialize a static array, i.e.

int a[4] = {2, 5, 6, 4};

What I mean is that even the following is illegal:

int a[4];
a = {1, 2, 3, 4}; //Error

In your case you can do nothing but copy the velue of each element by hand

for (int i = 1; i<=size; ++i)
{
    grid[i-1] = i;
}

You might avoid an explicit loop by using stl algorithms but the idea is the same

Some of this may have become legal in C++0x, I am not sure.


@above grid points to the address location where the first element of the array grid[] is stored. Since in C++ arrays are stored in contiguous memory location, you can walk through your array by just incrementing grid and dereferencing it.

But calling grid an (int*) isnt correct though.