.exe has stopped working

Hi all,
I have the following code:
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream.>
//#include<conio.h>
using namespace std;
int main()
{
char *p="Difficult";
char ch;
ch=++*p++;
cout<<ch<<*p;
//getch();
return 0;
}


When I run it on Windows with code::blocks (MinGW), the opening of console is succeeded by an error message "xyz.exe has stopped working" where xyz.cpp is the source. Yet, it works fine on Turbo C++ 3 on Windows (with some modifications of course like including conio.h and getch() as shown above). On Linux (With codeblocks and g++ 11), the console starts giving no output. What's wrong with the code? Why is it working only on Turbo C++? What the issue when I am using it in codeblocks?

Gramercy...
The expression ++*p++ has undefined behavior.

This is the best I can figure out:
1
2
3
4
5
6
7
char s[] = "Difficult";
char *p = s;
char ch = *p++;
//ch == 'D'
//p == "ifficult"
++*p;
//p == "jfficult" 
Last edited on
Topic archived. No new replies allowed.