Given the following code, I'm trying to understand if the pointer manipulation is legit:
struct Foo{
int *temp1;
}
temp1 => 2d array
struct Foo1{
int temp1[2][2];
}
temp1 => 3d array
struct Foo2{
int temp1[3][2][3];
}
I assign value to Foo1 and Foo2 using static data. For e.g.:
Foo1 f1 =
{
{ 2, 4 },
{ 1, 3 }
};
Foo2 f2 =
{
{
{
{101, 102, 103},
{104, 105, 106},
},
{
{107, 108, 109},
{110, 111, 112},
},
{
{113, 114, 115},
{116, 117, 118},
},
}
};
Can I reference Foo data from Foo1 like this:
Foo f;
f.temp1 = (int*)f1.temp1;
for(int i = 0; i < 2; ++i)
{
for(int j = 0; j < 2; ++j)
{
cout << "i " << i << " j " << j << " value: " << f.temp1[(i * 2) + j] << endl;
}
}
Can I reference Foo data from Foo2 like this:
Foo f;
f.temp1 = (int*)f2.temp1;
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 2; ++j)
{
for(int k = 0; k < 3; ++k)
{
cout << "i " << i << " j " << j << " k " << k << " value: " << f.temp1[(i * 3 * 2) + (j * 2) + k] << endl;
}
}
}
Essentially, I am assuming the array is going to arranged in contiguous memory and can I dereference it like this ?