Thread: array question!

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    array question!

    hi,...just wanted to know that how would the output of the following question is coming out to be..65480 and 65496....if it is given that the array begins at 65472.
    hope some1 can help me in understanding this!!!the question is :
    Code:
    main()
    {
    int a[3][4]=
                      {
                               1,2,3,4,
                                4,3,2,1,
                                7,8,0,0
                      };
           printf("\n%u %u",a+1,&a+1);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you solve it yourself? Print the address of each element of the array, and print out the sizeof an integer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
       printf("\n%u %u",a+1,&a+1);
    /*
    [545 Warning] Suspicious use of &
    [626 Warning] argument no. 2 inconsistent with format
    [626 Warning] argument no. 3 inconsistent with format
    */
    %u is used with an unsigned int.
    %p is used with a pointer (to void*).
    %d is used with an int.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    i'm not being able to understand the logic behind it's output?...that how did that come and what would be the ouptput if we just mention the name of the array and when we use & with the name of array,i know it prints the adrress of an array,but isn't that print the address like the base address of an array...?then why is it printing that as its output?i couldn't understand...can some1 plz help me out there... ...

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Try this for a bit.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    i'm sorry....couldn't understand,,....badly need a help plz!!!!!!!

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You need to understand data types.

    This maybe isn't the best, but have a go at it.
    Code:
    #include <stdio.h>
    
    int main()
    {
       int a[3][4]=
       {
          {1,2,3,4},
          {4,3,2,1},
          {7,8,0,0},
       };
    
       /* A supporting cast of characters (ooh, the puns). */
       int (*x)[4] = a;
       int (*y)[3][4] = &a;
    
       unsigned long sa = sizeof a;
       unsigned long sx = sizeof *x;
       unsigned long sy = sizeof *y;
    
       /* A fixed version of the original. */
       printf("%p %p %p\n", (void*)a, (void*)(a + 1), (void*)(&a + 1));
    
       /* Show that all three have or point to the same location. */
       printf("%p %p %p\n", (void*)a, (void*)x, (void*)y);
    
       /* Show how big each is. */
       printf("sa = %lu (0x%X), sx = %lu (0x%X), sy = %lu (0x%X)\n",
              sa, sa, sx, sx, sy, sy);
    
       /* So incrementing the starting location by the size of an object is... */
       printf("%p %p %p\n", (void*)a, (void*)(x + 1), (void*)(y + 1));
    
       return 0;
    }
    
    /* my output
    0012FF54 0012FF64 0012FF84
    0012FF54 0012FF54 0012FF54
    sa = 48 (0x30), sx = 16 (0x10), sy = 48 (0x30)
    0012FF54 0012FF64 0012FF84
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    couldn't understand properly,i'm confused now,i guess it would be better if u plz explain me through my example about the way it has reached to the output..of 65480 65496 if the array begins at 65472.....

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by galmca
    couldn't understand properly,i'm confused now,i guess it would be better if u plz explain me through my example about the way it has reached to the output..of 65480 65496 if the array begins at 65472.....
    Hmm. Values respectively 8 and 24 past the start?

    Well, it might have something to do with the sizes of integers, arrays of integers, and arrays of arrays of integers.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf("\n%u %u",a+1,&a+1)

    Ok, a+1 first
    a is the array name, which in this context is a pointer to the first element of the array, that being &a[0]
    Since pointer arithmetic always uses the size of the object the pointer is pointing to (in this case, it's "pointing" to an array of 4 ints (the minor dimension)), a+1 is really the same as &a[1]
    The result of this is a+1 (or &a[1]) is 8 bytes away from the start of the array.


    Now for &a+1
    Rather than pointing to the first element, this is a pointer to the whole array (who's size is 3 * 4 * sizeof(int) - which I guess is 2 on your system). This results in 24 bytes away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM