random_shuffle error

so im trying to understand how random_shuffle works and was testing it out. when i copy an example from the book that show how it work, and try to compile it, the .exe ran and got an error. here is the code
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>
using namespace std;

int main() 
{ char* s="ABCDEFGHIJ"; 
	cout << s << '\n';
	for(int i=0; i<4; i++)
	{ random_shuffle(s,s+10);
	  cout << s << '\n';
	}
}

this is what the output could be
ABCDEFGHIJ
CIJDBEAHGF
CFBDEIGAHJ
IDJABEFGHC
DBJIFEGACH

does anyone here know what is wrong with code above? there was no error or warming and the program should had ran but it didnt
Try char s[]="ABCDEFGHIJ"; instead of char *s="ABCDEFGHIJ";
Your program crashes because char *s="ABCDEFGHIJ"; declares a pointer to read-only memory that holds "ABCDEFGHIJ" while char str[]="ABCDEFGHIJ" puts the "ABCDEFGHIJ" into read-only memory and copies the string to newly allocated memory on the stack.
Last edited on
Topic archived. No new replies allowed.