array,c++

#include <iostream>
using namespace std;
int main()
{
int a[2][2][2];
int m;
cout<<"enter the value of array";
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
cin>>m;
a[i][j][k]=m;
}
}
}
int *p;
p=a; // why we can't assign the base adress of array to pointer

cout<<endl<<*(*(*(p+1)+1)+0)<<endl<<a[1][1][0];
return 0;
}
// why we can't assign the base adress of array to pointer
Because it is not an array of int. It is an array[2] of array[2] of arrays[2]. And it can be converted to pointer to array[2] of arrays[2]:
1
2
int (*p)[2][2];
p=a;
cannot understand this what you want to explain
Last edited on
You cannot assign it for same reason that you cannot do following:
1
2
double d;
int* p = &d;
Type mismatch.

Rules cor conversions tells us that array of T can be converted to pointer to T.
You are trying to assign a to int pointer, so for this conversion to be correct a should be array of ints.
But it is not. It is an array which contains inside another arrays which contains arrays of ints.
It can be converted only to pointer to array of arrays of ints. Which is uncompatible with pointer to int.
Registered users can post here. Sign in or register to post.