www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/11/08/17:42:12

Message-Id: <199711082239.JAA13318@rabble.uow.edu.au>
Subject: Re: Program with functions
To: SL AT usemail DOT com
Date: Sun, 9 Nov 1997 09:39:53 +1100 (EST)
Cc: djgpp AT delorie DOT com (DJGPP)
In-Reply-To: <199711082153.QAA22413@delorie.com> from sl at "Nov 8, 97 04:58:25 pm"
From: Brett Porter <bporter AT rabble DOT uow DOT edu DOT au>
MIME-Version: 1.0

> 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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019