Need Help
Hey guys, I have a piece of code to reverse a null-terminated string here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include "stdafx.h"
#include "conio.h"
#include "stdio.h"
#include <iostream>
using namespace std;
void reverse(char *str){
char *end = str;
char tmp;
if(str){
while(*end){ //find end of the string
++end;
}
--end; //set one char back, since last char is null
while(str < end){
tmp = *str;
*str++ = *end;
*end-- = tmp;
}
}
}
void main(){
char *myChar = "abcdefg";
reverse(myChar);
cout << myChar << endl;
_getch();
}
|
There always is error at "*str++ = *end;".
I don't know why.
Thanks so much.
I think the problem is in line below, you are trying to modify a piece of read only memory.
char *myChar = "abcdefg";
Let us know if you don't know how to fix it.
Topic archived. No new replies allowed.