Teen Programmers Unite  
 

 

Return to forum top

Calling a function with itself as an argument

Posted by Talon [send private reply] at August 18, 2001, 03:12:55 PM

I'm having trouble figuring this out, i'm using C.. Say i have a function 'left' that returns so many characters of a string starting from the left..

char *left(char *src, int length) {
char blah[1000];
strcpy(blah, src);
blah[length]='\0';
return(blah);
}

How do i make it so i can do something like:

printf("%s", left(left(left("Testing", 6), 4), 2));

without it going all screwy on me? Basically i need some way to use a different string in place of blah every time, so it's not using the same memory every time or something.. I dunno, HELP!

Posted by TheTutor [send private reply] at August 18, 2001, 03:59:41 PM

Well for starters you're returning the address of a local variable (this is a no-no)

I'd simply recreate the function like this:

void left(const char *src, char *dest, int len);

Fill dest with what you want and all is good -- You can even check really quick to see if dest is a valid pointer, if not you can malloc() off some memory for it :)

Posted by Talon [send private reply] at August 18, 2001, 04:29:55 PM

That doesn't make it so the function can call itself though, that wouldn't work at all if i tried:

printf("%s", left(left(left("Testing", 6), 4), 2));

because left wouldn't be returning anything.. it wouldn't work as an argument to itself..

Posted by CHollman82 [send private reply] at October 01, 2001, 10:15:50 PM

Posted by Talon [send private reply] at August 18, 2001, 05:02:16 PM

I know what it's called.. Every example i've seen doesn't have a recursive function that returns a value though.. I've never seen an example of a function calling itself using itself as an argument, which is the whole reason i'm posting here..

Posted by taubz [send private reply] at August 18, 2001, 05:43:08 PM

Your problem is that if you want to use the newly created string outside of your function, you have to allocate space for it outside of the function so that it still exists when the function exits..

If you do it this way...

char *left(char *src, int length) {
src[length]='\0';
return src;
}

... you'll have no problem, but you'll end up destroying the original string. Just make a copy of the string beforehand.

Note that this is *not* an example of recursion (CHollman!). The function isn't calling itself at all. You are calling the function several times, and using the return of the function as a parameter to the next call. It can be written out as...

a = left(a, 6);
a = left(a, 4);
a = left(a, 2);

...which clearly shows it has nothing to do with recursion.

- taubz

Posted by TheTutor [send private reply] at August 18, 2001, 06:00:16 PM

Didn't know you wanted the signature of the function to be the same -- You could do what taubz said, but you'd alter the src string as well and kinda defeat the whole purpose of calling the function -- You could create a local variable with "static" before it, and this will allow the variable NOT to be destroyed when you exit the function

Posted by CHollman82 [send private reply] at October 01, 2001, 10:15:58 PM

Posted by Psion [send private reply] at August 19, 2001, 09:00:25 PM

The ideal way is to pass an output buffer pointer, as suggested, and return the address of that same buffer. You can then nest the calls as you wanted.

Posted by Talon [send private reply] at August 22, 2001, 12:35:42 PM

Haha sweet it's actually working :P Thanks

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.