The problem is in the
char* temp = "abc"
and in the imperfect type system of C++.
int ints[3] = {1, 2, 3};
is actually an array of 3 integers, whose initial values are 1,2,3. The int[] is not const and the value can be changed (as you do when assigning
i=j; j=t;
in your
swap
.
char* temp="abc",
is actually a pointer to a char, that is initializad to point to the first character of the string literal
"abc"
that is treated as a constant.
(yes: its type should be
const char*
but is also convertible to
char*
to retain C backward compatibility).
Your
i=j
is trying to write into a read-only memory page.
Try
char temp[4] = {'a', 'b', 'c', '\0' };
Should work as with the int do.