Output Explanation

This is a discussion on Output Explanation within the C Programming forums, part of the General Programming Boards category; 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......

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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, 02:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 02:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 03: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, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21