0

What is wrong in this function?

   void stringReverse (char stringa[])
    {
        if (stringa[0]!='\0')
        {
            return stringReverse(&stringa[1]);
            printf("%c", stringa[0]);
        }
    }

I have to write a function that invert a string (ex: "Hello" in "olleH") using recursion; the function has to receive a string (nothing else) and to print the character in the inverse order... i don't understand why what i write didn't print anything...

3
  • 2
    A void function returns ... well... nothing. Commented Jul 9, 2015 at 13:19
  • 2
    Look at those few lines: Will the printf() ever be reached? Commented Jul 9, 2015 at 13:21
  • Please avoid using tags in titles, i.e, don't prefix c questions with "c:" in the title. Commented Jul 11, 2015 at 8:02

2 Answers 2

2

return returns a value from the function and performs no further statements in that scope. Have a think about where you want that statement to be...

Not answering in full because this sounds like homework!

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

3 Comments

This should be a comment
I was inlclined to make it a comment, but I also think different treatment is neccessary for homework answers. Better for people to think that to just be told in those circumstances.
Thanks, I appreciated the fact that you didn't solve my problem but you make me think over the error I committed, that's is definitely better!
1

Just do not use return. Use stringReverse(&stringa[1]); instead of return stringReverse(&stringa[1]);

Because, the 'return' statement is using for return a value. But your function is void type, that means it returns nothing, it doesn't need.

Comments

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.