Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a two-dimensional dynamic array:
int **p=new int*[d1];
for(size_t i=0;i!=d1;++i)
  p[i]=new int[d2];


Now I need a subarray of it, like
int **b=&p[1][1]

(Of course this is not correct)
Then I could operate a block of p using b, such as
b[0][0]=10 is the same as p[1][1]=10,
b[2][3]=20 is the same as p[3][4]=20.
The question is:
How to construct such a pointer to the subarray of p?
Thank you!
Posted
Updated 23-Feb-10 20:42pm
v2

I think you cannot do it in a simple way, you've either to:
  • allocate memeory for the (d1-1) integer pointers and assign them properly.

or
  • use a function to remap indexes of the array.

:)
 
Share this answer
 
It can not go... :)
You can obtain the only int* to navigate at one row (column).
But you could write a class to implement such behavior (results) :)
 
Share this answer
 
Thank you all!

I finished it with CPallini's first tip. Though I expected no memory allocation for b, I at least reduced a dimension when allocating memory for b.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900