Thread: Output Explanation

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Output Explanation

    Code:
    int main()
    {
      int a[5]={1,2,3,4,5};
      int *p=(int *)(&a+1);
    
      printf("%d %d",*(a+1),*(p-1));
      return 0;
    }
    Output is 2 5
    Please explain...

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Most probably &a is the address of an array of 5 int's, so if you add 1 to it, you'll be one integer beyond the end of the array. Compare to &a[0] + 1, or a + 1
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    a is array of ints so in expression a+1 - it is casted to pointer to int and then moved (so a+1 points to the second element of the array
    when dereferencing*(a+1) - you get the value of the element - 2

    &a is pointer to array of ints, so when moving it (&a+1) ... could you continue?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I think you meant

    Code:
    int a[5] = { 1, 2, 3, 4, 5 };
    int * p = a + 1;
    
    printf("%d %d", *(a + 1), *(p - 1));
    The variable a can become a pointer in pointer contexts, rather than an array. The expression (&a + 1) results in a pointer to pointer, which explains why you needed the cast.

    You're assigning p to an area after the end of the array, and when you dereference *(p - 1) you get your output because of pointer arithmetic. I think the compiler decided to backtrack by the size of your array.

    This question is one example why it's not smart to cast in C unless you're sure it's right.

  5. #5
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Anyone else please?
    I need explanation with *(p-1)
    How it is giving 5 as output.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by anirban View Post
    Anyone else please?
    I need explanation with *(p-1)
    How it is giving 5 as output.
    Because that's what it is. p points to one int past the end of the array, so backing up one int with p-1 points at the last int in the array, which not coincidentally has a value of 5.

  7. #7
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Yes thanks but how p is pointing one int past the end of the array.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by anirban View Post
    Yes thanks but how p is pointing one int past the end of the array.
    Code:
    &a            &a+1
     |              |
     v              v
    +--------------+-
    | whole array  |Next byte
    +--------------+-
    So p points to the next byte after the whole array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Remember, pointer arithmetic moves by the thing pointed to. If p is a pointer to int (which it is), then p-1 moves back one int. Since &a is a pointer-to-array-of-five-ints, &a + 1 moves forward one array of five ints, which moves it past the entire array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM