user10293779
user10293779

Reputation: 103

Error on Multiple Array Dynamic Memory Allocation

please advise why below do not work. use VC2017:

long **l;
l = new long [5][7];

it shows error as:

"a value of type "long*[7]" can not be assigned an entity of long**"...

How can I solve it?

Upvotes: 1

Views: 28

Answers (1)

dWinder
dWinder

Reputation: 11642

You need to declare and init the first array of pointer to long* and then assign to each his own array as:

long** l = new long*[5]; // declare array of pointer of 5 cell
for(int i = 0; i < 5; ++i)
    l[i] = new long[7]; // assign to each cell array with 7 cells

Remember that anything allocated with new is created on the heap and must be de-allocated with delete.

Upvotes: 1

Related Questions