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>
usingnamespace 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;
}
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.
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.