Thread: the two dimension c code

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    24

    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
    they call "address". what is that address?

    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. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by learning_grc View Post
    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.
    Last edited by BillyTKid; 10-09-2011 at 03:17 PM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by learning_grc View Post
    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
    they call "address". what is that address?

    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. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Your %P is also wrong and UB, for the right answer see above.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BillyTKid View Post
    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.
    Last edited by CommonTater; 10-09-2011 at 03:33 PM.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    %P is UB in ANSI C therefore it is UB.
    Your (void*) cast is also newbie stuff.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BillyTKid View Post
    %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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-11-2011, 02:49 AM
  2. array dimension
    By julianluthor in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2003, 05:01 PM
  3. Two dimension Array
    By jimiexe in forum C Programming
    Replies: 1
    Last Post: 10-14-2003, 03:22 PM
  4. Is this correct about the 4th dimension?
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 12-20-2002, 02:47 PM
  5. More on Two-Dimension Arrays..Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2002, 01:17 PM