Mail Archives: djgpp/1997/11/08/17:42:12
> Hi,
>
> I'm experiencing problems using functions. For the last while I've been using:
>
> char *add(char *first, char*second)
> {
> int result;
> result=first+second;
> return(result);
> }
>
This, I assume is meant to take two arguments, like the strings "1" and "20"
and return "21", is it not?
Well this won't work, because your routine just adds two pointers.
What you need is:
char *add( char *first, char *second )
{
int firstnum, secondnum, result;
char string[20], *retval;
firstnum = atoi(first);
secondnum= atoi(second);
result = firstnum+secondnum;
itoa( result, string, 10 );
retval = (char* )malloc( strlen(string)+1 );
strcpy( retval, string );
return retval;
}
> Problem is that RESULT is a local variable and when you return it it gets destroyed before it reachs
> the caller (because it is a local variable and naturally all local variables are destroyed when the function is
> complete.) So, how am I suppose to make this function work? In reality, the REAL function I'm working on is:
> "File.getname(BYTE mode)" whereby File is a class and getname() returns different components of the
> filename depending on MODE (i.e. name, extention, drive etc.)
>
If the above doesn't help, you are probably going to have to post the actual
function File.getname(BYTE mode), so that the DJGPP newsgroup readers don't
have to strain their psychic powers too much :)
Regards,
Brett
--
"Those here who believe in telekenesis... raise MY hand!"
--
Brett Porter -- blp01 AT uow DOT edu DOT au
http://www.geocities.com/SiliconValley/Park/7483
Programming Stuff - DJGPP - The PAGOS Project
http://www.geocities.com/CollegePark/Union/3596
Humour, University Life
- Raw text -