0
#include <iostream>
using namespace std;
int main()
{
 char str[] {"TESTING"};
 char *p {str};
 cout << (p++, *++p);
 cout << *p++;
 cout << p;
 return 0;
}

It returns "SSTING"

I know maybe this post isn't exactly for stackoverflow but I can't figure out what it does, and couldn't find any documentation about it

 cout << (p++, *++p); 

First time I saw round brackets with comma in cout... what's their function?

and shouldn't this line alone say "TESTING" but it seems to say only TING

cout << p;

Thank you!

1
  • the result of (a,b,c) is c. If that helps Commented Dec 6, 2016 at 0:21

2 Answers 2

2

Let's go line by line:

char str[] {"TESTING"};

This line defines a variable named str of type array of 8 chars, and initializes it with the characters TESTING plus a NUL char to mark the end.

char *p {str};

This one defines a variable named p of type pointer to char and initializes it to the address of the first char of the array str (the first T). This happens because the array automatically decays into a pointer in most uses.

cout << (p++, *++p);

This line does several things. The , operator first evaluates the left-hand operator p++, that increments the pointer, now points to the E; then it evaluates the right-hand operator *++p, but that is a pre-increment operator so it increments the pointer again (it points to S). Finally the * operator accesses to the memory pointed to by p, the result is a S. And that character is printed into STDOUT.

cout << *p++;

This one is easy. The * operator accesses the char pointed to by p (the S again) and prints it in STDOUT. Then it increments the pointer, because it is a post-increment operator. Now it points to the second T.

cout << p;

And at least, this line prints the string pointed to by p until it finds a NUL character. Since p is pointing to the second T of your array it will print TING.

Putting all those outputs together you get SSTING.

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

7 Comments

Thanks a lot for your detailed answer. Could you explain again the , operator please? Why it printed S between the two (E, S)
The , operator is actually simple. The expression E, S is evaluated as follows: first evaluate E and discard the result, then evaluate S and use the result as the value of the full expression. Usually the E is there for the side-effects, not for its actual value.
Evaluate means in this case... ? So basically it just prints out the second part of ,?
@user5372775: Not exactly. Evaluate means: compute the value and run the side effects. The ++ operator (both prefix and postfix) has a side effect, that is the increment of the pointer. If you do x = (3, 4); that is silly and the 3 does nothing. But if you do x = (p++, *p); then p is first incremented and then it is dereferenced. It would be just like: p++; x = *p;.
Oh and I wonder if I could ask you about these small stuff in the site's chat (if it has) because it's not worth it creating new threads
|
1

Not exactly an answer, but a breakdown of what was the code doing,

#include <iostream>

using namespace std;

int main()
{
    char str[]{"TESTING"};
    char *p{str}; // p points to: 'T'
    p++;          // p points to: 'E'
    ++p;          // p points to: 'S'
    cout << *p;   // output a single char: 'S'
    cout << *p;   // ouptut a single char: 'S'
    p++;          // p points to: 'T'
    cout << p;    // output a (char *) type pointer, AKA a C-string, "TING";

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.