M
Michael Jarrod
hi,
I have the following code piece of code:
#include <stdio.h>
#include <string.h>
void reverse(char* begin, char* end)
{
char tmp;
while(begin < end)
{
tmp = *begin;
*begin = *end;
*end = tmp;
++begin;
--end;
}
}
int main(int argc, char* argv[])
{
char a_str[8] = "abc def";
a_str[7] = '\0';
printf("str: %s\n",a_str);
reverse(a_str,a_str + strlen(a_str) - 1);
printf("str: %s\n",a_str);
char* b_str = "abc def";
printf("str: %s\n",b_str);
reverse(b_str,b_str + strlen(b_str) - 1);
printf("str: %s\n",b_str);
return 0;
}
when I use a_str for the parameter to the function reverse
everthing is ok, however when b_str is used the program
crashes when the line containing *begin = *end is executed.
I've always thought that the definitions of a_str and b_str
were more or less equivelent. Can someone explain where I
have gone wrong.
-Mich
I have the following code piece of code:
#include <stdio.h>
#include <string.h>
void reverse(char* begin, char* end)
{
char tmp;
while(begin < end)
{
tmp = *begin;
*begin = *end;
*end = tmp;
++begin;
--end;
}
}
int main(int argc, char* argv[])
{
char a_str[8] = "abc def";
a_str[7] = '\0';
printf("str: %s\n",a_str);
reverse(a_str,a_str + strlen(a_str) - 1);
printf("str: %s\n",a_str);
char* b_str = "abc def";
printf("str: %s\n",b_str);
reverse(b_str,b_str + strlen(b_str) - 1);
printf("str: %s\n",b_str);
return 0;
}
when I use a_str for the parameter to the function reverse
everthing is ok, however when b_str is used the program
crashes when the line containing *begin = *end is executed.
I've always thought that the definitions of a_str and b_str
were more or less equivelent. Can someone explain where I
have gone wrong.
-Mich