Multidimensional array, one dim fix, one dim dynamic

F

Florian Kaufmann

This of course works:

double* p = new double[10];

But now instead a double I want an array of 4 doubles. So the first
attempt would be do simply replace double by double[4]

(double[4])* p = new (double[4])[10];

But that doesn't compile. I tried a lot of different syntaxes. Is it
possible at all?
 
S

SG

This of course works:

double* p = new double[10];

But now instead a double I want an array of 4 doubles. So the first
attempt would be do simply replace double by double[4]

(double[4])* p = new (double[4])[10];

But that doesn't compile. I tried a lot of different syntaxes. Is it
possible at all?

Yes. It's

double (*p)[4] = new double[10][4];

Or with a typedef:

typedef double da4[4];
d4 *p = new d4[10];

Cheers,
SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top