# Thread: the two dimension c code

1. ## the two dimension c code

Could you please tell me what the code below is exactly doing?
particularly I'm confused about a[i][j]=2.....what is this??

And in last line for which data type they used "%8u" and something

Code:
```main ()
{
int a[3][2];
int i,j;
for (i=0; j<3; i++)
{
for (j=0; j<2; j++)
{
a[i][j] =2;
}
}
for (i=0; i<3; i++)
{
for (j=0; j<2; j++)
{
printf ("value in array %d\n", a[i][j]);
}
}
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
print ("value in array %d and address is %8u\n", a[i][j], &a[i][j]);
}
}
}```
orignal from : java2s.com

2. Originally Posted by learning_grc
orignal from : java2s.com
Its a joke or?
Java programmers should write java code not C.
%8u should display the address, is undefined behaviour therefore -> Java programmers should write java code not C.
Right answer to this horrible java-C code is %p in place of %8u.

int a[3][2] defines an 2dim array with 3 rows and 2 columns of int values.

3. Originally Posted by learning_grc
Could you please tell me what the code below is exactly doing?
particularly I'm confused about a[i][j]=2.....what is this??

And in last line for which data type they used "%8u" and something

Code:
```main ()
{
int a[3][2];
int i,j;
for (i=0; j<3; i++)
{
for (j=0; j<2; j++)
{
a[i][j] =2;
}
}
for (i=0; i<3; i++)
{
for (j=0; j<2; j++)
{
printf ("value in array %d\n", a[i][j]);
}
}
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
print ("value in array %d and address is %8u\n", a[i][j], &a[i][j]);
}
}
}```
orignal from : java2s.com
First of all lets fix this lousy text setup... and fix some errors...

Code:
```int main (void)
{
int a[3][2];
int i,j;

for (i=0; j<3; i++)
for (j=0; j<2; j++)
a[i][j] =2;

for (i=0; i<3; i++)
for (j=0; j<2; j++)
printf ("value in array %d\n", a[i][j]);

for (i=0; i<3; i++)
for (j=0; j<3; j++)
print ("value in array %d and address is %P\n", a[i][j], (void*) &a[i][j]);

return 0;
}```
Now you should actually be able to SEE what's going on ....

a[3][2] is a two dimensional array of 3 rows and 2 colums.

However the address should be printed with the %P specifier in that last line.
An address is just that... the location where something is stored...
just like on a street, your street address is where you are stored.

4. Your %P is also wrong and UB, for the right answer see above.

5. Originally Posted by BillyTKid
Its a joke or?
Java programmers should write java code not C.
%8u should display the address, is undefined behaviour therefore -> Java programmers should write java code not C.
Right answer to this horrible java-C code is %p in place of %8u.

int a[3][2] defines an 2dim array with 3 rows and 2 columns of int values.
No java programmers should not be prevented from learning C... in fact they *should* learn C ... it would expose them to the nuts and bolts of programming and make them better java programmers.

@BillTKid... no %P is NOT wrong... in C-99 %p prints lower case hex letter, %P prints them in upper case. And for %P or %p printf() expects a void pointer, hense the typecast.

6. %P is UB in ANSI C therefore it is UB.
Your (void*) cast is also newbie stuff.

7. Originally Posted by BillyTKid
%P is UB in ANSI C therefore it is UB.
Your (void*) cast is also newbie stuff.
So, do you now plan to run from thread to thread throwing "standards" arguments at me?
Trust me you don't want to do that...

Jailhouse politics do one thing and one thing only... they tick me right off.

Here's a quarter... go call someone who cares.