0

I'm just learning C and I'm trying to write a function that reverses a char-array (string) not with indices, but via a pointer and without malloc:

#include <stdio.h>
#include <string.h>

char* reverse(char* chararray)
{    
    char *forward = chararray;
    char *reversed = &chararray[strlen(chararray)-1];

    while (reversed > forward) {
        char revTmp = *reversed;
        char fwdTmp = *forward;

        *reversed-- = fwdTmp;
        *forward++ = revTmp;
    }   

    return reversed;
}

int main()
{
    char string[] = "dlrow olleh";
    printf("%s\n",reverse(string));
    return 0;
}

the output for now is world and should be hello world - but it breaks in the middle and I have no idea, why. There must be something I missed.

Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
  • 1
    Possible duplicate of [Reversing a string in C using pointers?](http://stackoverflow.com/questions/7544387/reversing-a-string-in-c-using-pointers) – Vineet1982 Feb 13 '17 at 10:42
  • 1
    @Vineet1982 - The point of duplicates is that they answer the given question. How does the proposed dup accomplish that? The OP is using a vastly different implementation. – StoryTeller - Unslander Monica Feb 13 '17 at 10:43

2 Answers2

2

reversed will point to the middle of the string after a successful reversal. So you will print only what's from that position in the string onward (in this case " world"). You can verify this by printing string as well, and see that it was indeed reversed.

Return the original pointer chararray instead.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
1

You are returning reversed, which points to the middle of your string.

You don't need to return anything, because you are reversing the input string in place.

In other words, you are using the same memory block (string) both for input and for output.


Just call the function, and then print that string:

reverse(string);
printf("%s\n",string);

In addition to that, you may as well change this:

char* reverse(char* chararray)
{
    ...
    return reversed;
}

To this:

void reverse(char* chararray)
{
    ...
}
barak manos
  • 29,648
  • 10
  • 62
  • 114
  • yep correct, but I want to return the pointer to do e.g. something like `printf("%s\n", reverse("dlrow olleh"));` – Matthias Burger Feb 13 '17 at 10:52
  • @MatthiasBurger: You can, but what's the point? A function declaration should tell the user everything about its purpose. Your current declaration implies that it might be allocating a new buffer for the output (because it makes little sense for a function to return its input exactly as is). For a second, just assume that you provide your code as a "black box" (e.g., header file + lib file) for someone else to use it. They will see only the function prototype in the header file, and assume that it must be allocating a new buffer for the output, which later needs to be deallocated. – barak manos Feb 13 '17 at 10:57
  • @MatthiasBurger - Careful. What you do in the comment has undefined behavior since you attempt to modify a literal in place. – StoryTeller - Unslander Monica Feb 13 '17 at 10:57
  • @MatthiasBurger: And of course, as mentioned in the comment above, passing a **read-only** string to your function is most definitely out of the question here (but that is regardless of my previous point). – barak manos Feb 13 '17 at 11:00
  • that's what I tried in future... see my post here: http://codereview.stackexchange.com/questions/154928/malloc-and-free-of-a-char-array - now I'm confused :D – Matthias Burger Feb 13 '17 at 11:16
  • @MatthiasBurger: What does that post have to do with your question here??? – barak manos Feb 13 '17 at 11:17
  • I tried using malloc and free - the person who answered on codereview told me not to use this. So I tried. now you are telling me to use malloc. – Matthias Burger Feb 13 '17 at 11:19
  • @MatthiasBurger: I did not say that you should use `malloc`, I just said that you don't need to return any value from your function, unless you use `malloc`. And since your problem-definition **requires** not to use `malloc`, you have to reverse the string using its own memory, and so you don't need to return it (because it "stays" at the same address). – barak manos Feb 13 '17 at 11:19
  • sorry @barakmanos misundestood the comments.. yeah of course... :/ – Matthias Burger Feb 13 '17 at 11:22
  • @MatthiasBurger: You should learn how to draw the line between requirements and implementation. In your question here, "not using `malloc`" was part of **the requirements**. The implementation that follows this requirement is that no value needs to be returned from the function. In that other post of yours, there was no such requirement, and you were free to use `malloc` in order to implement the solution. – barak manos Feb 13 '17 at 11:23