1

Having a really small problem. This is a pointer program I tried to make for practice, but I'm getting a error in Visual C++.

#include "stdafx.h"
#include "iostream"
#include "string"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char *p = "School";
    char c;
    c = ++*p++;
    cout << c << ", " << p << endl;
    cout << p << ", " << ++*p-- << ", " << ++p*++;    //Error C2059: syntax error : ';'
    return 0;
}

Maybe it's a very silly problem, but I can't seem to put my finger on it.

  • 6
    Whatever the cause of the syntax error, and regardless of whether they have well-defined behavior or not, `++*p++` and `++*p--` are just plain horrible expressions. Don't use them. – Mat Feb 22 '12 at 12:57
  • Just for the sake of clarity for persons that have to maintain your code (or yourself in some weeks), I would never write stuff like `++*p--` and `++p*++`. – hochl Feb 22 '12 at 12:58
  • I'm pretty sure it's illegal, too. Maybe this is the root of the problem. – Alexander Feb 22 '12 at 12:58
  • 2
    Clearly, don't do that. Never. And it's `const char* p = "School";`. – J.N. Feb 22 '12 at 13:00
  • @J.N. Most compilers will only give you a warning about the lack of `const` there. It's not a syntax error, just bad practice. – Nathan Fellman Feb 22 '12 at 13:02
  • What is a "pointer program" anyway. And what has it got to do with c++ – sehe Feb 22 '12 at 13:06
  • 1
    @NathanFellman : I disagree, it is a syntax error. It is just tolerated by some (= MSVC ?) compilers. See `-fno-const-strings` in GCC help: it mentions the standards requires it. – J.N. Feb 22 '12 at 13:06
  • @J.N.: `char *p = "School";` is invalid as of C++11. It's allowed in C++03, but deprecated and stupid, which is why even in C++03 mode gcc by default enables `-Wwrite-strings`, which warns for it. I think most people still use C++03. – Steve Jessop Feb 22 '12 at 13:13
  • @J.N.: Actually, it's allowed up to C++11. There's a conversion from a string literal to a `char*`. It's deprecated, and it's only there in order to allow old an terrible code to keep compiling, writing through the non-`const` pointer will certainly invoke _Undefined Behavior_, and I agree that one should never employ this conversion. But it is syntactically correct code. – sbi Feb 22 '12 at 13:15

1 Answers1

4

The problem is probably this:

++p*++

in the last cout. It looks like you switched the p and the *.


Now that we've put that aside, using multiple expressions with side effects in the same line is a recipe for trouble. What are you trying to do with this?

c = ++*p++;

or this?

cout << p << ", " << ++p-- << ", " << ++p++; //Error C2059: syntax error : ';'

The order of evaluating these statements is undefined.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
  • `++*p++` is not undefined. There are no sequence point issues, because two different objects are incremented, `p` and `*p`. It's still hell to read, though, and the `cout` line (or rather, something similar to it that actually compiles) is UB because the two separate `p++` can be ordered by the implementation without an intervening sequence point. – Steve Jessop Feb 22 '12 at 13:05
  • Oops. Sorry, `++*p++` *is* UB, because `p` points to a string literal so you can't modify `*p`. But the order is OK. – Steve Jessop Feb 22 '12 at 13:11
  • 1
    @Manav: http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points. And to elaborate on string literals: you must not modify a string literal. – Steve Jessop Feb 22 '12 at 13:17
  • @SteveJessop: doesn't `*` (as in dereferencing a pointer) have the same precedence wrt to both prefix and postfix `++`? If so, what's the difference between `++*p` and `*p++`? – Nathan Fellman Feb 22 '12 at 14:15
  • 1
    @Nathan: It does have lower precedence than both, but nevertheless its operand is always on the right hand side of it. So `++*p` is `++(*p)` (it increments `*p`), whereas `*p++` is `*(p++)` (it increments `p`). `++*p++` is `++(*p++)`, so `p` is post-incremented, and `*p` (the result of `*p++`) is pre-incremented. – Steve Jessop Feb 22 '12 at 14:19