2

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)

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
Luca
  • 1,658
  • 4
  • 20
  • 41
  • What output do you expect? – Petr Jul 30 '15 at 09:06
  • I think that I'm dereferencing cp before it get incremented. So I'm expecting it to print the entire string. – Luca Jul 30 '15 at 09:08
  • You are printing `'\0'` to the `cout` stream. This is UB as pointed by some people in [this question](http://stackoverflow.com/questions/30888851/what-does-cout-na-n-do/30888904). – Mohit Jain Jul 30 '15 at 09:09
  • "So I'm expecting it to print the entire string". It is printing the entire string, it's just inserting an extra newline also (and a `\0`) – Aaron McDaid Jul 30 '15 at 09:33
  • no, because the first call to cout is independent of the second call. Maybe my code isn't clear enough. But I found my error so thank you anyway. – Luca Jul 30 '15 at 09:37

4 Answers4

4

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;
}
Richard Dally
  • 1,432
  • 2
  • 21
  • 38
0

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 ++.

Nishant
  • 1,635
  • 1
  • 11
  • 24
0

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).

Alex Celeste
  • 12,824
  • 10
  • 46
  • 89
  • whoa, yes I got it, the first increment happens no matter what in the while condition.. thanks a lot! – Luca Jul 30 '15 at 09:14
0

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++;
Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69