cant figure the output..

Discussion in 'C Programming' started by nichas, May 18, 2005.

  1. nichas

    nichas Guest

    the code is ...
    int main(void){
    static int a[]={1,2,3,4,5};
    static int *p[5]={a,a+1,a+2,a+3,a+4};
    int i;
    for(i=0;i<5;i++){ printf("%u\n",a+i);}
    printf("\n%u\t%u\t%d",p,*p,*(*p));
    return 0;
    }
    Now in this code the output which iam getting is :::
    the address of p is 4 added to the address of element 5 in the array
    'a'. That means p points to the address of the memory location next to
    a[4] (since on my machine int is 4 bytes). But what i guessed is that
    it would point same as *p that is the element a[0]. I am not able to
    think how this can happen.. please reply where i am wrong in my
    thinking.
     
    nichas, May 18, 2005
    #1
    1. Advertising

  2. nichas

    S.Tobias Guest

    nichas <> wrote:

    > the code is ...


    #include <stdio.h> /* printf */
    > int main(void){
    > static int a[]={1,2,3,4,5};
    > static int *p[5]={a,a+1,a+2,a+3,a+4};
    > int i;
    > for(i=0;i<5;i++){ printf("%u\n",a+i);}

    printf("%x\n", (void*)(a+i))
    > printf("\n%u\t%u\t%d",p,*p,*(*p));

    printf("\n%x\t%x\t%d\n", (void*)p, (void*)*p, *(*p))
    > return 0;
    > }
    > Now in this code the output which iam getting is :::


    You should have included your output. Mine was:

    -|8049490
    -|8049494
    -|8049498
    -|804949c
    -|80494a0
    -|
    -|80494a4 8049490 1

    > the address of p is 4 added to the address of element 5 in the array
    > 'a'. That means p points to the address of the memory location next to
    > a[4] (since on my machine int is 4 bytes).


    In my output it is the same. But this is, of course, a coincidence,
    the address might be different.

    `p' is an array (of ptrs to int), it is not a pointer, therefore
    it doesn't point to anything. In the expression it decays to "a
    pointer to the first element", therefore is also the address of
    the second array, which happens to be adjacent to the first one
    (and this is the reason for the difference of 4).

    >But what i guessed is that
    > it would point same as *p that is the element a[0].


    In `*p', again `p' decays into "the pointer to the first element";
    `*' and `p' together then yield an lvalue for the first element
    (or simply `p[0]'). The first element is `a'; the identifier
    denotes the array, but in the expression it decays into a pointer
    to its first element (the value of `a' is also the address of the
    first array). So `*p' is a pointer to the first element of `a' (&a[0])
    (the first number in the output and the second one in the last line
    are the same).

    It's easy now to guess that `**p' is the same as `a[0]' (the last
    output number is "1").

    To sum up:

    p == &p[0]
    *p == p[0] == a == &a[0]
    **p == a[0]

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, May 18, 2005
    #2
    1. Advertising

  3. nichas wrote:
    > the code is ...
    > int main(void){
    > static int a[]={1,2,3,4,5};
    > static int *p[5]={a,a+1,a+2,a+3,a+4};
    > int i;
    > for(i=0;i<5;i++){ printf("%u\n",a+i);}
    > printf("\n%u\t%u\t%d",p,*p,*(*p));
    > return 0;
    > }
    > Now in this code the output which iam getting is :::
    > the address of p is 4 added to the address of element 5 in the array
    > 'a'. That means p points to the address of the memory location next to
    > a[4] (since on my machine int is 4 bytes). But what i guessed is that
    > it would point same as *p that is the element a[0]. I am not able to
    > think how this can happen.. please reply where i am wrong in my
    > thinking.
    >

    You need to check your textbook for
    1) required headers for variadic functions (e.g. printf)
    2) pointer arithmetic
    3) proper specifiers for pointers with printf.

    Check the following code, that differs from yours in important ways, and
    see if it, along with your textbook, helps you understand what is happening:

    #include <stdio.h>
    int main(void)
    {
    static int a[] = { 1, 2, 3, 4, 5 };
    static int *p[5] = { a, a + 1, a + 2, a + 3, a + 4 };
    int i;
    for (i = 0; i < 5; i++)
    printf("a + %d = %p, &a[%d] = %p, p[%d] = %p\n", i,
    (void *) (a + i), i, (void *) &a, i, (void *) p);
    printf("p = %p, *p = %p, p[0] = %p, **p = %d, *p[0] = %d\n",
    (void *) p, (void *) *p, (void *) p[0], **p, *p[0]);
    return 0;
    }


    a + 0 = b6f4, &a[0] = b6f4, p[0] = b6f4
    a + 1 = b6f8, &a[1] = b6f8, p[1] = b6f8
    a + 2 = b6fc, &a[2] = b6fc, p[2] = b6fc
    a + 3 = b700, &a[3] = b700, p[3] = b700
    a + 4 = b704, &a[4] = b704, p[4] = b704
    p = b6e0, *p = b6f4, p[0] = b6f4, **p = 1, *p[0] = 1
     
    Martin Ambuhl, May 19, 2005
    #3
  4. nichas

    nichas Guest

    >
    > #include <stdio.h>
    > int main(void)
    > {
    > static int a[] = { 1, 2, 3, 4, 5 };
    > static int *p[5] = { a, a + 1, a + 2, a + 3, a + 4 };
    > int i;
    > for (i = 0; i < 5; i++)
    > printf("a + %d = %p, &a[%d] = %p, p[%d] = %p\n", i,
    > (void *) (a + i), i, (void *) &a, i, (void *)

    p);
    > printf("p = %p, *p = %p, p[0] = %p, **p = %d, *p[0] = %d\n",
    > (void *) p, (void *) *p, (void *) p[0], **p, *p[0]);

    Can u tell me why we need to cast p, *p to the void pointers...
    One more thing where is p pointing to. Its address in ur program
    is b6e0 which is before a[0]. Is there any logic or p can point
    anywhere.. please reply.

    > return 0;
    > }
    >
    >
    > a + 0 = b6f4, &a[0] = b6f4, p[0] = b6f4
    > a + 1 = b6f8, &a[1] = b6f8, p[1] = b6f8
    > a + 2 = b6fc, &a[2] = b6fc, p[2] = b6fc
    > a + 3 = b700, &a[3] = b700, p[3] = b700
    > a + 4 = b704, &a[4] = b704, p[4] = b704
    > p = b6e0, *p = b6f4, p[0] = b6f4, **p = 1, *p[0] = 1
     
    nichas, May 19, 2005
    #4
  5. nichas

    -berlin.de Guest

    nichas <> wrote:
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> static int a[] = { 1, 2, 3, 4, 5 };
    >> static int *p[5] = { a, a + 1, a + 2, a + 3, a + 4 };
    >> int i;
    >> for (i = 0; i < 5; i++)
    >> printf("a + %d = %p, &a[%d] = %p, p[%d] = %p\n", i,
    >> (void *) (a + i), i, (void *) &a, i, (void *)

    > p);
    >> printf("p = %p, *p = %p, p[0] = %p, **p = %d, *p[0] = %d\n",
    >> (void *) p, (void *) *p, (void *) p[0], **p, *p[0]);

    > Can u tell me why we need to cast p, *p to the void pointers...


    Because the "%p" format specifier expects a void pointer according to
    the C standard.

    > One more thing where is p pointing to. Its address in ur program
    > is b6e0 which is before a[0]. Is there any logic or p can point
    > anywhere.. please reply.


    If you use 'p' in value context it's taken to mean the address of the
    first element of the array 'p', i.e. the address of p[0], and not the
    content of what's stored in p[0], which would be the address of a[0].

    Regards, Jens

    PS: Could you please try to avoid these "u" and "ur" abbreviations?
    They make things harder to read for non-native speakers like me
    and it shouldn't take you too much time to type "you" or "you're"
    instead. Thank you.
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, May 19, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AFN
    Replies:
    9
    Views:
    445
  2. johny
    Replies:
    5
    Views:
    431
    Karl Heinz Buchegger
    Apr 14, 2005
  3. Justin
    Replies:
    2
    Views:
    276
    Justin
    Oct 9, 2006
  4. Simon Mansfield

    Cant quite figure out whats wrong!? Help??

    Simon Mansfield, Dec 5, 2004, in forum: C Programming
    Replies:
    6
    Views:
    412
    Lawrence Kirby
    Dec 6, 2004
  5. Nagaraj
    Replies:
    1
    Views:
    914
    Lionel B
    Mar 1, 2007
Loading...

Share This Page