Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
The following program converts 3D array into 1D array with help of pointers . Some conversion error is coming . The line in which error is coming contains assignment operator with pointer to pointer to int type on both sides .
#include<iostream>
using namespace std ;
int main()
{
// REPLACING 3D ARRAY WITH A 2D ARRAY AND THEN INTO A 1D ARRAY USING               POINTERS  .  

int abc [2][2][3] ;
int **p[3][2] ;
int *c[6] ;
//          // abc gives address of first element of 3d array ie first 2d   array .

// abc is pointer to pointer to pointer to int type .

int i , j ;     // * abc represents address of first 1d array of first 2d array .

for (i=0 ; i<=2 ; i++) // *abc +1:address of second 1d array of first 2d 
{                            // array .
for (j=0 ; j<=1 ; j++)
{
p[i][j] =  *(abc+i )  + j ; // Conversion error comes here , but WHY ?

} 
}                  

for (i=0 ; i<=5 ; i++) 
{
for (j=0 ; j<=1 ; j++ )     
{
c[i] = *p[i][j] ;     
}

}

// entering array elements .
for (i=0 ; i<=5 ; i++)
{
cin>>* c[i] ;   

}

// required array elements .
for (i=0 ;i<=5 ;i++)
{
cout<<*c[i]<<"    "; // 3d array elements are accessed using 1d array  
}                                                        // of pointers .
}


What I have tried:

I have tried to run this program on dev c++ but displays following error :

[Error] cannot convert 'int (*)[3]' to 'int**' in assignment
Posted
Updated 15-Jan-17 20:55pm
v2
Comments
Philippe Mori 13-Jan-17 16:04pm    
Do one thing at a time... Start by a simple program and write one line of code at a time.

By the way the compiler error message clearly explain that it cannot convert an array of integer to a pointer to a pointer to an integer.
David O'Neil 14-Jan-17 21:02pm    
Conceptualize a 3d array in your head. I haven't stepped through your code, but you seem to be trying to treat part of it as a 2d array for your first step. And if I understand it correctly, you are treating a 2x3 array as a 3x2 array, which aren't the same. Before fixing that, I don't believe you can convert a 3d array into a 2d array the way you believe you can. Next, as you've defined the array, it is already linear in memory, if you want a master trick. But your teacher probably wants you to create a second linear array in memory, to show that you understand pointers. When doing so, you need to copy values out of the existing array by using the [][][] nomenclature, not pointer nomenclature, if you want to keep it simple. (You can use pointer nomenclature when you know what you are doing, but it will be much different than what you have. Use the [] method to master it first.)

1 solution

It is pretty obvious, when you would understand the mechanismins in array access. Your abc is an 3-dimensional array of integers.

valid code is:
C++
int value =  abc[i][j][k] ;//get value with array access
int *pointer =  &(abc[i][j][k]);//get pointer

It is easier to work with the array access and avoid big pointer arhitmetics
 
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