How to reference 3D Array?

I

Immortal Nephi

The array has 2D matrix. 2D matrix contains two rows and two
columns. I want to create multiple 2D matrixs into array. It looks
like 3D array. My code is easier to select which matrix I want to
use.
How do I use reference to pick up 2D matrix?

int main()
{
unsigned char xx[ 2 ][ 2 ][ 2 ] =
{
{ // First Matrix
{ 1,2 },
{ 3,4 }
},
{ // Second Matrix
{ 5,6 },
{ 7,8 }
}
};

unsigned char (*x)[2] = &xx[ 0 ][ 0 ][ 0 ]; // Error Matrix 1

unsigned char y1 = x[0][0];
unsigned char y2 = x[0][1];
unsigned char y3 = x[1][0];
unsigned char y4 = x[1][1];

(*x)[2] = { &xx[ 1 ][ 0 ][ 0 ] }; // Error Matrix 2

unsigned char y5 = x[0][0];
unsigned char y6 = x[0][1];
unsigned char y7 = x[1][0];
unsigned char y8 = x[1][1];

return 0;
}
 
D

Daniel Pitts

Immortal said:
The array has 2D matrix. 2D matrix contains two rows and two
columns. I want to create multiple 2D matrixs into array. It looks
like 3D array. My code is easier to select which matrix I want to
use.
How do I use reference to pick up 2D matrix?

int main()
{
unsigned char xx[ 2 ][ 2 ][ 2 ] =
{
{ // First Matrix
{ 1,2 },
{ 3,4 }
},
{ // Second Matrix
{ 5,6 },
{ 7,8 }
}
};

unsigned char (*x)[2] = &xx[ 0 ][ 0 ][ 0 ]; // Error Matrix 1

unsigned char y1 = x[0][0];
unsigned char y2 = x[0][1];
unsigned char y3 = x[1][0];
unsigned char y4 = x[1][1];

(*x)[2] = { &xx[ 1 ][ 0 ][ 0 ] }; // Error Matrix 2

unsigned char y5 = x[0][0];
unsigned char y6 = x[0][1];
unsigned char y7 = x[1][0];
unsigned char y8 = x[1][1];

return 0;
}

&xx[0] would be matrix 1 and &xx[1] would be matrix 2.

&xx[0][0] is the first row of the first matrix. &xx[0][0][0] is the
first cell in the first row of the first matrix.
 
I

Immortal Nephi

Immortal said:
   The array has 2D matrix.  2D matrix contains two rows and two
columns.  I want to create multiple 2D matrixs into array.  It looks
like 3D array.  My code is easier to select which matrix I want to
use.
   How do I use reference to pick up 2D matrix?
int main()
{
   unsigned char xx[ 2 ][ 2 ][ 2 ] =
   {
           { // First Matrix
                   { 1,2 },
                   { 3,4 }
           },
           { // Second Matrix
                   { 5,6 },
                   { 7,8 }
           }
   };
   unsigned char (*x)[2] = &xx[ 0 ][ 0 ][ 0 ]; // Error Matrix 1
   unsigned char y1 = x[0][0];
   unsigned char y2 = x[0][1];
   unsigned char y3 = x[1][0];
   unsigned char y4 = x[1][1];
   (*x)[2] = { &xx[ 1 ][ 0 ][ 0 ] }; // Error Matrix 2
   unsigned char y5 = x[0][0];
   unsigned char y6 = x[0][1];
   unsigned char y7 = x[1][0];
   unsigned char y8 = x[1][1];
   return 0;
}

&xx[0] would be matrix 1 and &xx[1] would be matrix 2.

&xx[0][0] is the first row of the first matrix. &xx[0][0][0] is the
first cell in the first row of the first matrix.

Why can't you show me example how to reference matrix 1? I prefer to
use 2D on x after 3D on xx gives matrix's memory address to x.
 
D

Daniel Pitts

Immortal said:
Immortal said:
The array has 2D matrix. 2D matrix contains two rows and two
columns. I want to create multiple 2D matrixs into array. It looks
like 3D array. My code is easier to select which matrix I want to
use.
How do I use reference to pick up 2D matrix?
int main()
{
unsigned char xx[ 2 ][ 2 ][ 2 ] =
{
{ // First Matrix
{ 1,2 },
{ 3,4 }
},
{ // Second Matrix
{ 5,6 },
{ 7,8 }
}
};
unsigned char (*x)[2] = &xx[ 0 ][ 0 ][ 0 ]; // Error Matrix 1
unsigned char y1 = x[0][0];
unsigned char y2 = x[0][1];
unsigned char y3 = x[1][0];
unsigned char y4 = x[1][1];
(*x)[2] = { &xx[ 1 ][ 0 ][ 0 ] }; // Error Matrix 2
unsigned char y5 = x[0][0];
unsigned char y6 = x[0][1];
unsigned char y7 = x[1][0];
unsigned char y8 = x[1][1];
return 0;
}
&xx[0] would be matrix 1 and &xx[1] would be matrix 2.

&xx[0][0] is the first row of the first matrix. &xx[0][0][0] is the
first cell in the first row of the first matrix.

Why can't you show me example how to reference matrix 1? I prefer to
use 2D on x after 3D on xx gives matrix's memory address to x.
&xx[0] is matrix the first matrix, I already told you.
 
A

Anton

Immortal said:
The array has 2D matrix. 2D matrix contains two rows and two
columns. I want to create multiple 2D matrixs into array. It looks
like 3D array. My code is easier to select which matrix I want to
use.
How do I use reference to pick up 2D matrix?
int main()
{
unsigned char xx[ 2 ][ 2 ][ 2 ] =
{
{ // First Matrix
{ 1,2 },
{ 3,4 }
},
{ // Second Matrix
{ 5,6 },
{ 7,8 }
}
};
unsigned char (*x)[2] = &xx[ 0 ][ 0 ][ 0 ]; // Error Matrix 1
unsigned char y1 = x[0][0];
unsigned char y2 = x[0][1];
unsigned char y3 = x[1][0];
unsigned char y4 = x[1][1];
(*x)[2] = { &xx[ 1 ][ 0 ][ 0 ] }; // Error Matrix 2
unsigned char y5 = x[0][0];
unsigned char y6 = x[0][1];
unsigned char y7 = x[1][0];
unsigned char y8 = x[1][1];
return 0;
}

&xx[0] would be matrix 1 and &xx[1] would be matrix 2.

&xx[0][0] is the first row of the first matrix. &xx[0][0][0] is the
first cell in the first row of the first matrix.

Why can't you show me example how to reference matrix 1? I prefer to
use 2D on x after 3D on xx gives matrix's memory address to x.

I guess, you want something like this:

int main()
{
unsigned char xx[ 2 ][ 2 ][ 2 ] =
{
{ // First Matrix
{ 1,2 },
{ 3,4 }
},
{ // Second Matrix
{ 5,6 },
{ 7,8 }
}
};

unsigned char (*x)[2] = xx[ 0 ]; // Matrix 1

unsigned char y1 = x[0][0];
unsigned char y2 = x[0][1];
unsigned char y3 = x[1][0];
unsigned char y4 = x[1][1];

x = xx[ 1 ] ; // Matrix 2

unsigned char y5 = x[0][0];
unsigned char y6 = x[0][1];
unsigned char y7 = x[1][0];
unsigned char y8 = x[1][1];

return 0;
}
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Immortal said:
The array has 2D matrix. 2D matrix contains two rows and two
columns. I want to create multiple 2D matrixs into array. It looks
like 3D array. My code is easier to select which matrix I want to
use.
How do I use reference to pick up 2D matrix?

int main()
{
unsigned char xx[ 2 ][ 2 ][ 2 ] =
{
{ // First Matrix
{ 1,2 },
{ 3,4 }
},
{ // Second Matrix
{ 5,6 },
{ 7,8 }
}
};

unsigned char (*x)[2] = &xx[ 0 ][ 0 ][ 0 ]; // Error Matrix 1

unsigned char y1 = x[0][0];
unsigned char y2 = x[0][1];
unsigned char y3 = x[1][0];
unsigned char y4 = x[1][1];

(*x)[2] = { &xx[ 1 ][ 0 ][ 0 ] }; // Error Matrix 2

unsigned char y5 = x[0][0];
unsigned char y6 = x[0][1];
unsigned char y7 = x[1][0];
unsigned char y8 = x[1][1];

return 0;
}

const int a = 2, b = 3, c = 4;
unsigned char xx[a][c] = { /* initialisation skipped /* };
unsigned char *zero = &xx[0][0][0];
unsigned char (*one)[c] = &xx[0][0];
unsigned char (*two)[c] = &xx[0];
unsigned char (*three)[a][c] = &xx;
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAktVbbIACgkQm4klUUKw07CI9gCcCo3BdoalC1uPYzmj9gcAT11l
kkcAn18Kd9HircUyKd0H2Y+D+y0ctnJ4
=/Ayg
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top