I can't seem to get the syntax correct for a pointer to pointer to const length array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David Frank

    #1

    I can't seem to get the syntax correct for a pointer to pointer to const length array

    For a runtime calculated dimension of size NX the following syntax is correct.

    Code:
      double (*var)[2] = new double[NX][2];
    But what I also want is a runtime dimensioned array of pointers to these guys but I can not find the proper syntax. Something akin to the following (which is wrong).

    Code:
      double *(*var)[2] = new double*[NX][2];
      for(i=0; i<NX; ++i)
        {
        var[i] = new double[NY][2]; // NY != NX
        }
    What is the correct syntax?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This dynamically allocates a 2D array.

    var is a pointer to an array of 2 doubles.

    Code:
        double (*var)[2] = new double[NX][2];
    Now to allocate an array of pointers to arrays of 2 doubles you code:

    Code:
    	double* (*ptr)[2] = new double* [NX][2];
    Now ptr is an array of pointers that contain addresses of arrays if 2 doubles.

    Finally, you assign to the ptr array by:

    Code:
    *(ptr[0]) = var[0];
    since ptr[0] is an address of a pointer it must be that
    *(ptr[0]) is element 0 of the ptr array. Therefore, you assign var[0] (which is the address of an array of 2 double) to *(ptr[0]) which is also the address of an array of 2 double.

    Comment

    Working...