while(*ptstring++ != '

This is a discussion on while(*ptstring++ != '

Thread: while(*ptstring++ != '\0'); help

'); help
within the C Programming forums, part of the General Programming Boards category; if ptstring is a pointer to a memo block of strings i saw this statment in a book and assumed ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    20

    while(*ptstring++ != '\0'); help

    if ptstring is a pointer to a memo block of strings i saw this statment in a book and assumed it would be the same as

    while(*ptstring != '\0')
    ptstring++;

    but its not and if i change it to the latter the program will crash.!
    i dont remember seeing a code like that before specialy with a

    " ; " at the end of a while bracket,

    dumbfunded.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    because the former still increments the pointer even if the condition is false. Your version does not.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Never worry about writing concise code. Worry only about writing self-documenting understandable code. Try not to use ++ or -- inside another expression.I know the language was designed in such a way as it actively encouraged you to write concise code but your intent becomes less obvious when you follow strictly the rules of operator precedence and it is easy to make silly mistakes that can go unnoticed.
    Dont know about most others here but almost every coding standard ive had to work to has actively discouraged this. I certainly prefer to maintain code that is easily understandable and always strive to write self-documenting code so that those who have to maintain my code dont have too hard a job on their hands.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You should be careful when using the * dereference with binary operators. Consider the folllowing:

    char s[] = "abc";
    char * p = s;
    if( *p + 1 == 'b' )
    //...


    Depending on your compiler, that may be interpreted as

    if(s[1] == 'b')

    or maybe even

    if(s[0]+1 == 'b')

    For this odd example, either would be true, of course.

    Anyway, parenthesis make things all the more clear:

    if( *(p + 1) == 'b' ) //...if(s[1] == 'b')
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Sebastiani
    You should be careful when using the * dereference with binary operators. Consider the folllowing:

    char s[] = "abc";
    char * p = s;
    if( *p + 1 == 'b' )
    //...


    Depending on your compiler, that may be interpreted as

    if(s[1] == 'b')

    or maybe even

    if(s[0]+1 == 'b')

    For this odd example, either would be true, of course.

    Anyway, parenthesis make things all the more clear:

    if( *(p + 1) == 'b' ) //...if(s[1] == 'b')
    doesn't operator order demuddify this one? unary * comes before binary +, so (*p + 1) should be the same as ((*p) + 1)
    hello, internet!

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Dont know about most others here but almost every coding standard ive had to work to has actively discouraged this.
    I agree with Stoned_Coder, writing readable and maintainable code is far more important than writing "clever" code. Also a lot of coding standards I've worked with didn't allow operations, including function calls, in conditions.

    So this

    if (CONSTANT == *a++)

    was not allowed and also this

    if (CONSTANT == function ())

    was not allowed.

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Sebastiani
    You should be careful when using the * dereference with binary operators. Consider the folllowing:

    char s[] = "abc";
    char * p = s;
    if( *p + 1 == 'b' )
    //...


    Depending on your compiler, that may be interpreted as

    if(s[1] == 'b')

    or maybe even

    if(s[0]+1 == 'b')

    For this odd example, either would be true, of course.

    Anyway, parenthesis make things all the more clear:

    if( *(p + 1) == 'b' ) //...if(s[1] == 'b')
    That's not true at all -- p is always dereferenced first.


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