reverse () function

The following is the testing code for the standard C++ generic algorithm reverse() taken verbatim from a reference book.It compiles (w/o errors or warnings in Dev_C++)but crashes immediately or after the first cout<< output. Can anyone see a reason for this behavior?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{                            //crashes here
    char* s = "ABCDEFGHIJ";
    cout<<"print string s \n";
    cout<<s;                 //.....or here
    reverse(s,s+10);
    cout<<"\nprint reversed string s \n";
    cout<<s;

system("pause");
return 0;    
}    

There is no urgency.
Last edited on
s is pointing to a constant part of memory: char* s = "ABCDEFGHIJ";
change it to an array: char s[] = "ABCDEFGHIJ"; and it should work
OK - I have changed the #include to <string> and it now prints the string before having to be closed with Ctrl+c.
.........and it did. Many thanks Bazzy. I will try to dig into this further to properly understand.
Last edited on
Bazzy meant that replace the variable type from char* s to a char array as the array you have given with a pointer can't be altered it is a constant value in memory.
Yes.....thank you kenshee I understood Bazzy.
Topic archived. No new replies allowed.