xdevel2000
xdevel2000

Reputation: 21374

c++ pointer to pointer of an int

If I have:

int **p;

why can't I do this?

p = new *int[4];

but if I have:

class T {...}
T **c;
c = new *T[4]; 

is that correct?

Upvotes: 1

Views: 109

Answers (3)

Paul Evans
Paul Evans

Reputation: 27577

You're trying to multiply the keyword new with the type (int or T)! To say you want a new array of pointers to int:

p = new int*[4];

or an array of pointers to T:

c = new T*[4];

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254631

The * has to come after the type-name that it modifies:

p = new int*[4];
c = new T*[4]; 

Upvotes: 9

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

No it is not correct.

the * must go after the type-name.

Then it should be:

p = new int*[4];

And

c = new T*[4];

Upvotes: 2

Related Questions