what difference between while(*p){p++;} ,while (*++p){;} and while(*p++){;}?

前端 未结 4 1792
日久生厌
日久生厌 2021-01-07 11:45

It\'s about strcat function.

while (*p)
    p++;

and

while (*++p)
    ;

both works, but

4条回答
  •  被撕碎了的回忆
    2021-01-07 12:03

    Let's assume that p is a string.

    while (*p) p++; /* (1) */
    while (*++p) ;  /* (2) */
    while (*p++) ;  /* (3) */
    
    • (1) is different from (2) if p is an empty string.
    • (1) is different from (3) because with (3), even if the current value of *p is a '\0' character, p is incremented.

提交回复
热议问题