Luca
Luca

Reputation: 1776

pointers and increment operator

I have the following simple code:

#include<iostream>

const char str[]={'C','+','+'};

int main()
{ 
  const char *cp=str;                     
  std::cout<<*str<<std::endl;

  while (*cp++>0)  
    std::cout<<*cp;
} 

can't understand why it prints

C
++

Shouldn't the postfix increment operator evaluate the expression but return the value unchanged? (I double checked precedence of increment, dereference and relational operators and it should work)

Upvotes: 2

Views: 166

Answers (4)

Alex Celeste
Alex Celeste

Reputation: 13390

Your problem is here:

while (*cp++>0)  
    std::cout<<*cp;

The postfix operator increments the value after the expression it was used in is evaluated. There are two different expressions referring to cp in this while statement though: the first one tests and increments, but the second one prints. Since the second one is a separate statement, the increment has already happened by then.

i.e. you currently test against 'C', then increment regardless of the result, then print '+' through the now-incremented pointer. If it were iterating a string literal, this code would also increment once after reaching the 0, although you don't see a result of that because it skips the print (because it iterates over a hand-created array with no null terminator, what it actually does here is fall off the end and exhibit undefined behaviour; this is a big error in itself, you can't rely on there being a zero at the end if one wasn't put there).

Upvotes: 0

Richard Dally
Richard Dally

Reputation: 1450

This line prints: C and the carriage return.

std::cout<<*str<<std::endl;

Then you are looping on following characters but there is no ending character (c.f. buffer overflow)
Here's the code fixed up.

#include <iostream>

const char str[]={'C','+','+', '\0'};

int main()
{ 
    const char* cp = str;                     
    std::cout<< *str << std::endl;

    while (*cp++ > 0)  
    std::cout << *cp;

    return 0;
}

This code is even simpler if you want to display "C++"

#include <iostream>

const char str[]={'C','+','+', '\0'};

int main()
{ 
    const char* cp = str;                     

    while (*cp > 0)
        std::cout << *cp++;

    std::cout << std::endl;

    return 0;
}

Upvotes: 4

Aswin Murugesh
Aswin Murugesh

Reputation: 11080

In the line

while(*cp++>0)

The post increment operator is executed, after the evaluation happens.

i.e. cp points to C during the first evaluation of the while condition.

So,

*cp => 'C' which is greater than 0.

Before moving to the next line (Inside the loop), the post increment operator gets executed, making cp point to the first +

After + is printed, the while condition is executed again, this time *cp returns '+'. since '+' > 0, the control enters the loop for the second time.

Before entering the loop, the post increment operator executes again, making cp point to the second '+', which is printed.

Now, while condition is executed for the third time. Here, *cp returns +. Thus, control enters the loop again.

Before entering the loop, post increment executes again. This time, it makes cp point to the next character, which is \0.

The \0 is printed, which makes no difference in this code. Then, when the while condition executes again, *cp returns \0, which is not > 0. So, the condition fails.

Edit: Saw in the comments that you wanted to print the entire String in the same line. Change the loop to:

while(*cp > 0)
    std:cout<<*cp++;

Upvotes: 0

Nishant
Nishant

Reputation: 1665

shouldn't the postfix increment operator evaluate the expression but return the value unchanged?

No, postfix operator returns the value of operand first then, only it is increased by 1.

here,

while (*cp++>0)  
    std::cout<<*cp;

by the time it reaches std::cout<<*cp; the value of cp is incremented, and hence the result ++.

Upvotes: 0

Related Questions