3

In the following code:

int strlen(char *s){
    char *p = s;

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

    return p - s;
}

Why does the above evaluate differently than this:

int strlen(char *s){
    char *p = s;

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

    return p - s;
}

It is my understanding that the expression will evaluate first, and then increment.

13
  • Do you understand that there's a difference between i++ and ++i? Commented Jul 14, 2013 at 5:23
  • Both increment the variable by 1, and result in the same answer if not in an expression. I can correctly use the the two in practice, but it just appeared to me that similar use with pointers yielded different results. As in: if(i++ > 5), the original value of i would be used, and incremented after the evaluation. Commented Jul 14, 2013 at 5:24
  • Because there's a difference between the two, but it's nothing to do with pointers. Try int i = 3, j = 3; printf("%d, %d\n", ++i, j++);. Commented Jul 14, 2013 at 5:25
  • I know, but the same concept when used on pointers does not seems to yield similar results. As in the use of *(++ptr), and *(ptr++). My question was, shouldn't the expression internal to the parenthesis be evaluated first since they have the highest precedence? If this is not the case, then it makes sense otherwise. Commented Jul 14, 2013 at 5:27
  • I just thought that, by use of the expression *(ptr++), than it would evaluate to the ptr++ first, and then dereference it. But it appears that I have assumed incorrectly. Commented Jul 14, 2013 at 5:30

3 Answers 3

10

in the first code p is incremented regardless of if the while() condition was true or false.

In the second snippet of code, p is incremented ONLY if while condition was true.

Sign up to request clarification or add additional context in comments.

2 Comments

Bhai : first can be rectified as while(*p++); return (p- s -1);
Surely bhai @GrijeshChauhan
2

Consider the last step in while loop when *p = '\0'.

In 1st code:

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

p still get one increment, and pointer to the element behind '\0'.

In 2nd code:

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

*p != '\0' is not true, so while loop end, p get no increment. p pointer to '\0'.

Comments

0

In the first case:-

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

p will be incremented just after the evaluation of an expression irrespective of whether the condition is true or false because ++ is a part of conditional expression.

Whereas, in the second one:-

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

First the condition will be checked and if it is true then only p will be incremented.

3 Comments

No, I think the comparison happens before the increment in both cases. ++p would do the increment before the comparison. But in the first case, the increment is executed even if the string is zero length.
@Sanjay, Oh... yes actually the increment will take place just after the condition is checked. No matter whether its true or false(First Case) . I forgot that's a post increment. ;)
the condition is first checked if *p contains \0 or not. If it does, p++ is incremented in the second snippet. Whereas, p will be incremented regardless of if the condition where true or false in the first snippet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.