1

I'm trying to reverse the string via void function, but segmentation fault occurs for "str" it's stored read-only memory (as I known from searching existing thread in the forum).

I tried to use strcpy(copy, str) in "reverse" function but it's just reversing the newly created char array "copy" which cannot be returned to main function. I don't want to use printf("%s", copy) in the reverse function to print the reversed string.

Is there anyway to reverse str

  1. Without changing it to char str[] = "Hello"
  2. Without changing the code in main() function
  3. Keep the function type of reverse() as void?
void reverse(char* str)
{
  int l = strlen(str);
  char temp;
  int j = length - 1;

  for (int i = 0; i < j; ++i)
  {
     temp = str[i];
     str[i] = str[j];
     str[j] = temp;
     --j;
  }
}

int main(int argc, char* argv[])
{
   char* str = "Hello";
   reverse(str);
   printf("%s\n", str);
   return 0;
}
SSpoke
  • 5,656
  • 10
  • 72
  • 124
mmcc88
  • 177
  • 1
  • 10
  • possible duplicate of [How do you reverse a string in place in C or C++?](http://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c) – Parappa Sep 24 '15 at 01:51
  • 2
    Short answer to (at least questions 1 & 3) is "no." A "char *" allocated like that is going to be read-only. You can't change the pointer with your current `reverse (char *str)` function signature. You either need a writable source array, or a way to allocate new memory in reverse (and return that new pointer.) – Joe Sep 24 '15 at 01:52
  • "Hello" is a string constant and has static scope, so the compiler may place it in read-only memory. If you want to reverse it you should make a copy of the data in main (using strcpy) and pass a pointer to the copied data into reverse(). – Parappa Sep 24 '15 at 01:53
  • You've listed three possible solutions. Is there any reason you don't want to use any of them? – user253751 Sep 24 '15 at 01:57
  • With the restrictions you've placed, you can't. You can't modify a string literal in C. – PC Luddite Sep 24 '15 at 02:06
  • well, I'm trying to figure out is there any other way that I missed out apart from these 3 possible solutions. – mmcc88 Sep 24 '15 at 02:13
  • @Matt I added another alternative to do this, please read my answer again. Two of the three requierements are really impossible. – Iharob Al Asimi Sep 24 '15 at 02:20
  • @iharob Perfect bro. – mmcc88 Sep 24 '15 at 02:29

2 Answers2

3

The problem is you cannot modify string literals.

Try like this:

char str[] = "Hello";
reverse(str);

above, str is no longer a string literal, it's an array initialized to {'H', 'e', 'l', 'l', 'o', '\0'}, so you can modify it's contents if you want.

Or you can use malloc()

char *str = malloc(6);
if (str != NULL)
 {
    strcpy(str, "Hello");
    reverse(str);
    fprintf(stdout, "%s\n", str);
    /* And never forget to `free' */ 
    free(str);
 }

When defining string literals use const to protect it from being modified, at least from accidentally doing so.

There is no way to satisfy these requirements

  1. Without changing it to char str[] = "Hello"

    Because you cannot modify string literals

  2. Without changing the code in main() function

    Because that would require the program to modify a string literal and YOU CAN'T

The third requirement, does not impose any problem at all.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
-4

everytime you use a pointer that you have to use a malloc() in C or new() in C++ which could probably be the issue. but you can always cast de-reference anything to its address back to a pointer like i did below

DEMO: http://ideone.com/LnOtDL

#include <string.h>

void reverse(char* str)
{
  int length = strlen(str);
  char temp;
  int j = length - 1;

  for (int i = 0; i < j; ++i)
  {
     temp = str[i];
     str[i] = str[j];
     str[j] = temp;
     --j;
  }
}

int main(int argc, char* argv[])
{
   char str[] = "Hello";
   reverse(&str[0]);
   printf("%s\n", str);
   return 0;
}
SSpoke
  • 5,656
  • 10
  • 72
  • 124
  • Yeah I know but hell it works.. even if it complicates the code.. I'm not a programmer i'm a reverse engineerer but I think i found the issue it's the pointer to string without a `malloc/new`. I'll try to fix up the answer to make it better – SSpoke Sep 24 '15 at 01:58
  • 2
    It doesn't work at all, it causes undefined behavior. – Iharob Al Asimi Sep 24 '15 at 02:00
  • ya it works now sorry I did that way to quick without even checking it even had typo's – SSpoke Sep 24 '15 at 02:04
  • 1
    It really doesn't work trust me, it might appear to work but it doesn't. Change the program a little bit leaving this part as it is, and it might do something very unexpected. – Iharob Al Asimi Sep 24 '15 at 02:06
  • Ok i will fix it.. i think it's better now, it's pointing to the first element in the array – SSpoke Sep 24 '15 at 02:14
  • 1
    `&str[0]` is redundant. `str` decays to a pointer when passed to a function which already points to the first element in the array. – PC Luddite Sep 24 '15 at 02:15
  • screw it i'll just quietly leave you guys are all above me I can't stay here too long. – SSpoke Sep 24 '15 at 02:17
  • 2
    @PCLuddite it adds to clarity of the code though, so not redundant. You could also argue that the spaces either side of the `=` signs are redundant. – M.M Sep 24 '15 at 02:37
  • It's actually hard for me to distinguish the three tokens with the lack of spaces. – Iharob Al Asimi Sep 24 '15 at 03:03
  • @M.M I would disagree. It makes sense to pass a pointer to a string to a function that requires a pointer to the string. `&str[0]` just clutters code. It's not the same as arguing that spaces are redundant because spaces make code easier to read. Surrounding a pointer with superfluous operators does not. – PC Luddite Sep 24 '15 at 05:52