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.

2
  • 1
    Possible duplicate of Reversing a string in C using pointers? Commented Feb 13, 2017 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. Commented Feb 13, 2017 at 10:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

oh damn, yes... you are right... never did such an easy mistake :D
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)
{
    ...
}

10 Comments

yep correct, but I want to return the pointer to do e.g. something like printf("%s\n", reverse("dlrow olleh"));
@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.
@MatthiasBurger - Careful. What you do in the comment has undefined behavior since you attempt to modify a literal in place.
@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).
that's what I tried in future... see my post here: codereview.stackexchange.com/questions/154928/… - now I'm confused :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.