Thread: Screen output of array elements

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    26

    Screen output of array elements

    Hi,

    Could anyone tell me in words what this code is implementing & perhaps tell me what is the screen output?

    I think there is an array of 6 elements, via using the pointer this functions is used to manipulate the number elements within the array - as in this case an extra 3 elements have been added to array therefore 9 elements. I am not too sure.....please help!


    Code:
    int arr[6] = {4, 2, 5, 11, 8, 14}
    int *p;
    p = arr + 3;
    printf("%d\n", *p);
    p--;
    printf("%d\n", *p);
    (*P)++;
    printf("%d\n", *(p+1));
    Thanks

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    This is nothing but pointer algebra.
    p=arr+3; means p points to the 4th element, so *p will give you 11. Then p-- will make p to decrement and thus *p will be 5. Then (*p)++ will increment the value pointed to by p which was 5 and thus *p will now be 6. The last one will still point to at 11 coz p wasn't changed but *p was.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    26
    Quote Originally Posted by BEN10 View Post
    This is nothing but pointer algebra.
    p=arr+3; means p points to the 4th element, so *p will give you 11. Then p-- will make p to decrement and thus *p will be 5. Then (*p)++ will increment the value pointed to by p which was 5 and thus *p will now be 6. The last one will still point to at 11 coz p wasn't changed but *p was.
    Okay,

    but does p=arr+3 simpley saying 1 + 3=4, 4th element which is 11? (i kno its obvious)


    last line of code [code]printf("%d\n", *(p+1));[\code] will still point to at 11? because *(p+1) does seem to confuse the situation.

    Cheers

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    When referencing an array object directly, like you do with 'p=arr+3', it returns a pointer to the first element in the array. When summing 3 to it, it is returning the 4th element in the array.

    So, basically, the print order will be like this:
    Code:
    11 (4th element)
    5 (3rd element)
    11 (4th element)

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    26
    Quote Originally Posted by abraham2119 View Post
    When referencing an array object directly, like you do with 'p=arr+3', it returns a pointer to the first element in the array. When summing 3 to it, it is returning the 4th element in the array.

    So, basically, the print order will be like this:
    Code:
    11 (4th element)
    5 (3rd element)
    11 (4th element)
    Understood thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding elements in array
    By reb221 in forum C Programming
    Replies: 6
    Last Post: 12-30-2008, 02:41 PM
  2. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM