By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

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

P: n/a
For a runtime calculated dimension of size NX the following syntax is correct.

Expand|Select|Wrap|Line Numbers
  1.   double (*var)[2] = new double[NX][2];
  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).

Expand|Select|Wrap|Line Numbers
  1.   double *(*var)[2] = new double*[NX][2];
  2.   for(i=0; i<NX; ++i)
  3.     {
  4.     var[i] = new double[NY][2]; // NY != NX
  5.     }
  6.  
What is the correct syntax?
Oct 15 '10 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
This dynamically allocates a 2D array.

var is a pointer to an array of 2 doubles.

Expand|Select|Wrap|Line Numbers
  1.     double (*var)[2] = new double[NX][2]; 
Now to allocate an array of pointers to arrays of 2 doubles you code:

Expand|Select|Wrap|Line Numbers
  1.     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:

Expand|Select|Wrap|Line Numbers
  1. *(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.
Oct 17 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.