Thread: recursive function question

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    11

    recursive function question

    Hi,

    I was trying to see why does this code works , i found it at another website. It is a function to reverse a string

    Code:
    char* reverse_str(char* s)
    {
    	char* reverse = malloc(1);
    	//char* reverse;
    
    	int i;
    
    	if(*s != '\0')
    		reverse = reverse_str(s+1);
    
    	i = strlen(s) - 1;
    
    	if (i >= 0)
    		reverse[i] = s[0];
    
    	return reverse;
    }
    my question is , why does this code works? because I see that each recursive call to the function assigns just one memory location to the "char *reverse " variable and then when the recursive calls end , the variable "char *recursive" is treated like if it was assigned more than one location.

    sorry for my english.
    Last edited by hkl01; 12-25-2007 at 09:44 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Define "works". This code is wrong.... horribly wrong.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    the code reverse all strings , i can't understand how the recursive memory assignment with malloc(1) creates consecutive memory locations in the same variable

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by hkl01 View Post
    the code reverse all strings , i can't understand how the recursive memory assignment with malloc(1) creates consecutive memory locations in the same variable
    It doesn't. It's a waste of memory that is allocated and goes nowhere.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I don't know what was in the head of the guy who wrote this. Defintely some ugly piece of code and ugly use of recursivity.

    Code is wrong because
    • Creates memory leaks
    • Writes in memory who doesn't belong to him
    • Doesn't append a '\0' character at the end of the reversed string
    • And, between me and you, it's ugly


    If you want to try to understand what the program is doing, well, you could try execute it with a debugger, might help you understand how it "works". But it's defintely one ugly piece of recursive code. I don't feel there's a simple way of writing this kind of function recursively by using only one fonction (in part because of the '\0' you need to append at the end of the string). But maybe there is.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Are you trying to reverse a character? Well, that's certainly nice.

    You could reverse a string very easily using 1 for loop.

    Code:
    void reverse(char str[])
    {
    int i;
    for(i = strlen(str); i > 0; i--)
    printf("%c", str[i]);
    }
    
    int main(void)
    {
    char str[10] = "HelloWorld";
    reverse(str);
    
    return 0;
    }
    Last edited by eXeCuTeR; 12-26-2007 at 01:00 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's not much better -- you're printing the NULL and ignoring the first character in the string. Plus strlen() returns a size_t. And it's not indented.

    Try something more along the lines of this: (codeformed)
    Code:
    #include <stdio.h>  /* for putchar() */
    #include <string.h>  /* for strlen() */
    
    void print_string_reverse(const char *str) {
        size_t x;
    
        for(x = strlen(str); x > 0; x --) {
            putchar(str[x - 1]);
        }
    }
    I don't think that was the OP's original question anyway . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function name basic question help
    By kenryuakuma in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 07:48 AM
  2. Need help designing a recursive function.
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-24-2008, 12:45 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM