CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Nov 2007
    Posts
    7

    Question Help with this C snippet

    i'm using TURBO C compiler on a Windows System

    Code:
     #include <stdio.h>
     main()
     {
        char *p1 = "New";
        char *p2;
        p2 = (char *)malloc(20);
        while(*p2++ = *p1++)
         ;
        printf("\n%s",p2)
     }
    o/p :
    Blank screen

    From my understanding p2 is pointing to one position after '\0' character.
    But why is the o/p appearing blank when it shud be a garbage value.
    I tried it on two different pc's running TURBO C but still got the same result.
    or is the blank character a garbage value..

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help with this C snippet

    Depends on your compiler settings. Under some modes, extra 0-padding is added even if you don't ask for it.

    My guess is that you're seeing garbage, the first char of which just happens to be 0.

  3. #3
    Join Date
    May 2008
    Posts
    96

    Re: Help with this C snippet

    Its because you are getting lucky.

    Are you deliberately trying to crash your program?

  4. #4
    Join Date
    Aug 2005
    Location
    UK
    Posts
    29

    Re: Help with this C snippet

    Quote Originally Posted by AnandD
    i'm using TURBO C compiler on a Windows System

    Code:
     #include <stdio.h>
     main()
     {
        char *p1 = "New";
        char *p2;
        p2 = (char *)malloc(20);
        while(*p2++ = *p1++)
         ;
        printf("\n%s",p2)
     }
    o/p :
    Blank screen

    From my understanding p2 is pointing to one position after '\0' character.
    But why is the o/p appearing blank when it shud be a garbage value.
    I tried it on two different pc's running TURBO C but still got the same result.
    or is the blank character a garbage value..
    Wouldn't you get an infinite loop ? while(*p2++ = *p1++)

  5. #5
    Join Date
    May 2008
    Posts
    300

    Re: Help with this C snippet

    Quote Originally Posted by stephenteh
    Wouldn't you get an infinite loop ? while(*p2++ = *p1++)
    Assignment expressions will give a value 0 from time to time.
    Nope

  6. #6
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Help with this C snippet

    There's a difference between ++p and p++!
    That's why p2 points to the string terminating NULL char. Learn to debug.
    Also, I believe it should be:
    Code:
    printf("%s\n",p2)
    not
    Code:
    printf("\n%s",p2)
    Best regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help with this C snippet

    Quote Originally Posted by Bornish
    There's a difference between ++p and p++!
    That's why p2 points to the string terminating NULL char.
    I think ++p *would* have it pointing to the terminating null, but p++ does not.....

  8. #8
    Join Date
    May 2008
    Posts
    300

    Re: Help with this C snippet

    There's a difference between ++p and p++!
    That's why p2 points to the string terminating NULL char. Learn to debug.
    And try to debug.

    All I recall there are two operator can break the evaluation of expression && and ||.
    Nope

  9. #9
    Join Date
    Aug 2005
    Location
    UK
    Posts
    29

    Re: Help with this C snippet

    Quote Originally Posted by DreamShore
    Assignment expressions will give a value 0 from time to time.
    Yes you are right, just verified it in ollydbg :-). And for the op's code the loop will test the value from p1 and break the loop when it's null. Learnt a new thing today (at least for me) :-)

  10. #10
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Help with this C snippet

    Quote Originally Posted by Lindley
    I think ++p *would* have it pointing to the terminating null, but p++ does not.....
    True... my bad.
    All he needs to do is to memset the p2 with some other char before the while...
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  11. #11
    Join Date
    Nov 2007
    Posts
    7

    Smile Re: Help with this C snippet

    Thanx ya'll

    i guess its a garbage value...

  12. #12
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Help with this C snippet

    Pehaps I'm being thick but I can't see anything with the OP code except the printf statement at the end:

    1) Missing ; after function call.
    2) Some systems don't do any output untill the next \n.
    My hobby projects:
    www.rclsoftware.org.uk

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Help with this C snippet

    Pehaps I'm being thick but I can't see anything with the OP code except the printf statement at the end:
    The most fundamental problem is that p2 itself is changed by the loop, but that was not accounted for. Aside from that and what you mentioned, other problems include the fact that <stdlib.h> is not included, main() is not declared as explicitly returning an int, p1 is not declared as pointing to a const char, and there is no free() to match the malloc().
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    May 2008
    Posts
    96

    Re: Help with this C snippet

    Thanks laserlight.

    Yes, at the end of the loop, p2 points to one-past the null terminator, so all bets are off when you try to printf() it.

    Sorry, I suppose I could have been much more helpful about that in the first post.

  15. #15
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Help with this C snippet

    Quote Originally Posted by laserlight
    The most fundamental problem is that p2 itself is changed by the loop, but that was not accounted for.
    Oh yes, so it is.


    Quote Originally Posted by laserlight
    Aside from that and what you mentioned, other problems include the fact that <stdlib.h> is not included, main() is not declared as explicitly returning an int, p1 is not declared as pointing to a const char, and there is no free() to match the malloc().
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured