Why does it say "Memory access violation"?
char* str = "HelloGuys";
int len = strlen(str);
for (int i=0; i<(len/2); ++i){
char t = str[len-i-1];
str[len-i-1] = str[i]; //error
str[i] = t;
}
String literals are stored in read only section of memory. Any attempt to modify the contents of a string literal invokes Undefined Behaviour (segmentation fault on most implementations).
Use an array of characters rather
char str[] = "HelloGuys";
As Prasoon already said, string literals are not modifiable.
If you need a modifiable array of chars have it like this:
char str[] = "HelloGuys";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With