Thread: Strange output ---need help

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    Question Strange output ---need help

    #include <stdio.h>

    main()
    {
    int arr[]={ 0,1,2, 3, 4,5, 6, 7, 8, 9};
    int *ptr=arr+1;
    print( ++ptr,ptr--,ptr,ptr++,++ptr);
    }

    print(int *a, int *b, int *c, int *d, int *e)
    {
    printf("\n%d , %d , %d , %d , %d \n", *a,*b,*c,*d,*e );
    }

    The output is as follows
    2 , 2 , 3 , 1 , 3

    Can anyone help me to understand the output.

    Thanks..Jack

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    What is this line supposed to do?

    >>> print( ++ptr,ptr--,ptr,ptr++,++ptr);

    If you want to print, use printf("%d, %d, %d, %d, %d", ++ptr, ptr--, ptr, ptr++, ++ptr);

    Also, this line makes no sense, it's like you are trying to declare pointers in a print statement.

    >>> print(int *a, int *b, int *c, int *d, int *e)

    The only line giving output is

    >>> printf("\n%d , %d , %d , %d , %d \n", *a,*b,*c,*d,*e );

    which just happens to grab info from random memory addresses since you never initialized the pointers.

    This is just what I think, I could be wrong.

  3. #3
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    First of all, I'm guessing that this is not something that you wrote, but rather a problem you have to figure out (I just don't see what you would be trying to do this way =)

    As to what this chunk of code does:
    Code:
    #include <stdio.h>
    
    void print(int *a, int *b, int *c, int *d, int *e);
    
    int main()
    {
       // creates an array of integers
       int arr[] = {0, 1, 2, 3, 4,5, 6, 7, 8, 9};
    
       // ptr gets the address of the second item in the array 
       int *ptr = arr + 1;
    
       // this sends the addresses of items before and after ptr
       print(++ptr, ptr--, ptr, ptr++, ++ptr);
    
       return (0);
    }
    
    void print(int *a, int *b, int *c, int *d, int *e)
    {
       printf("\n%d , %d , %d , %d , %d \n", *a, *b, *c, *d, *e);
    }
    Now, as to what the code does. You have declared an array of ints, [0-9], and then assigned the address of the second element to ptr. The "++" and "--" either add or subtract one from that value, and based on the position of them, this is done before or after the value is passed down to the function.

    When the "++" or "--" is placed BEFORE the variable, 1 is added/subtracted from the variable BEFORE the expression is evaluated. In the case where it is placed AFTER the variable, 1 is added/subtracted from the variable AFTER the expression is evaluated. Try an explanation under link below:
    http://www.nmr.mgh.harvard.edu/C/incr_decr.shtml

    If seems to me that some of this might be compiler-specific because I got different output on M$VC++ from that of DevC++, and different from yours.

    >>which just happens to grab info from random memory addresses since you never initialized the pointers.
    I'm pretty sure the pointers are initilized in the line:"print(int *a, int *b, int *c, int *d, int *e)"
    Last edited by dbaryl; 02-20-2002 at 10:22 PM.
    This is my signature. Remind me to change it.

  4. #4
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    One more thing, can someone run the code through their compiler and tell me what you get? 'Cause I got 2 different outputs from M$VC and my other compiler... don't know what's wrong...
    This is my signature. Remind me to change it.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I've got MSVC++ 6.0 and I got:
    3, 2, 3, 2, 3
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I've got GCC and my result:

    3, 3, 3, 2, 2

    Edit:

    Replace the print in main with:

    print(++ptr, ++ptr, ++ptr, ++ptr, ptr);

    And the result will be:

    5, 4, 3, 2, 1

    The reason for this is that variable passing starts with the first last parameter, in this case it is ptr. Then ptr is increased each time.

    Assume ptr has address 0000. The print can be seen as:

    print (0016, 0012, 0008, 0004, 0000);

    Since 0000 is the address of (arr+1), this means that *(0000) = 1.
    And that's why you get 5, 4, 3, 2, 1.

    Back to the original code:

    print(++ptr, ptr--, ptr, ptr++, ++ptr);

    First 2 is passed. Since this is *((arr+1)+1). Then again 2 is passed, since it is ptr++ and not ++ptr. After 2 is passed, the pointer is increased. So now *((an+1)+2) == 3 is passed. Then again 3 is passed and ptr is decreased. Then ptr is increased so it ends at the same as where it was, which is 3.
    Last edited by Shiro; 02-21-2002 at 03:34 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > print( ++ptr,ptr--,ptr,ptr++,++ptr);
    Such garbage as this has never had a meaningful outcome, so it really doesnt matter what codemangler 7.2 makes of it, its still broken code

    http://www.eskimo.com/~scs/C-faq/q3.1.html

  8. #8
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Thanks everyone, that clears it up for me. I had no idea that the last parameter is passed first... I thought that this kind of code was just pointless, but the reason I looked at it is because I thought that was an excercise, something to understand rather than use... Also, I guess different compilers interpret it differently...

    Well, thanks again.
    This is my signature. Remind me to change it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 03:56 AM
  2. Binary Search - Strange Output
    By mike_g in forum C Programming
    Replies: 13
    Last Post: 06-16-2007, 02:55 PM
  3. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 05:40 PM