Reputation: 45
I have a function that reverses a char array, but when it gets to a certain point, I receive the error. Can anyone help? I have looked, but have not found anything specific geared towards this, I think.
char* strrev( char* s )
{
char c;
char* s0 = s - 1;
char* s1 = s;
/* Find the end of the string */
while (*s1) ++s1;
/* Reverse it */
while (s1-- > ++s0)
{
c = *s0;
*s0 = *s1; // This is where I am receiving the Bad Access.
*s1 = c;
}
return s;
}
Upvotes: 1
Views: 144
Reputation: 434665
I'll wager a guess that you're calling your function like this:
char *s = strrev("pancakes");
If so, then you're trying to modify a string literal and many systems put string literals in read-only memory. If you do it like this:
char s1[] = "pancakes";
char *s2 = strrev(s1);
you should have better luck.
Upvotes: 2