Reputation: 2541
I came across this statement in a book.
new employee *[num];
where employee has already been declared as struct employee and num is an int;
this is on the right hand side of = operator on the statement.
So, what is meant by that statment? The book doesn't offer any explanation of the said statement.
Upvotes: 0
Views: 205
Reputation: 29744
This will allocate a memory required to hold num
number of pointers to employee
on the free store.
e.g:
employee** a = new employee* [2]; // 2 pointers on the heap
heap:
address a (e.g. 0x97a0008): pointer1 to employee
address a + 1 ( 0x97a000c): pointer2 to employee
Side note: you use delete[]
on arrays so you can delete above with delete[] a
BUT you will have to loop first through all entries if you have allocated memory for them so it have to be freed before you loose the pointer.
https://stackoverflow.com/a/13477214/1141471
Upvotes: 7
Reputation: 590
Think it is like this,
typedef employee* TEmployee;
TEmployee * ap = new TEmployee[10];
So, it allocates memory for dynamic array of size 10 type TEmployee (not employee). It is as simple as,
int * aip = new int[10];
TEmployee itself is a pointer type to employee.
Upvotes: 2