Multidimensional array, one dim fix, one dim dynamic

Discussion in 'C++' started by Florian Kaufmann, Apr 28, 2010.

  1. 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?
     
    Florian Kaufmann, Apr 28, 2010
    #1
    1. Advertising

  2. Florian Kaufmann

    SG Guest

    On 28 Apr., 15:08, Florian Kaufmann wrote:
    > 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
     
    SG, Apr 28, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page