You are here:

C++/Pointer access for 3D Array

Advertisement


Question
Hi Vijayan,

   can we access & modify a 3D array with a single pointer...?
   Something like this
   int arr[3][4][5];
   int ***ptr = (int ***)arr;

Thanks in Advance
Bhupal.

Answer
> can we access & modify a 3D array with a single pointer...?
There are no 3D arrays in C++; however you can have arrays of arrays of arrays.

> Something like this
>   int arr[3][4][5];
>   int ***ptr = (int ***)arr;

That is an in correct cast and will result in undefined  behaviour.

The following is valid C++

int main()
{
       int array[3][4][5];
       int (*pointer)[4][5] = array ;

       // the foolowing two lines are equvalent
       array[1][2][3] = 77 ;
       pointer[1][2][3] = 77 ;
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.