linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* how to implement routines that return general strings?
@ 2006-08-10 14:10 Robert P. J. Day
  2006-08-10 17:12 ` Scott
  2006-08-10 21:07 ` Glynn Clements
  0 siblings, 2 replies; 10+ messages in thread
From: Robert P. J. Day @ 2006-08-10 14:10 UTC (permalink / raw)
  To: C programming list


  i'm about to design and implement a number of routines that return
strings and, as far as i can tell, i have two general design choices.

  int		func(char* buf, int bufsiz) ;
  char*		func() ;

  that first form obviously accepts the address of a buffer (and its
size) into which to copy the result, and leaves the "int" return value
to be used perhaps as an error code.  perfectly reasonable, but it
forces every invocation to first have a buffer ready to go.

  the second form would be intuitively more natural, but is affected
by the scope of the result.  the approach i would use there might be:

char*
func()
{
	static buf[128] ;	// or whatever size is appropriate
	... copy result into buf ...
	return buf ;
}

an error of some kind might be implemented by returning NULL, but that
will restrict the range of error codes i can pass back.

  are there compelling arguments either way?  while i like the natural
feel of the second form, i have to admit the first form is more
informative.  thoughts?

rday



^ permalink raw reply	[flat|nested] 10+ messages in thread
* RE: how to implement routines that return general strings?
@ 2006-08-10 16:51 Khan, Ahmer
  0 siblings, 0 replies; 10+ messages in thread
From: Khan, Ahmer @ 2006-08-10 16:51 UTC (permalink / raw)
  To: Robert P. J. Day, C programming list

It really depends on what func() is doing inside. If it's only
allocating memory and copying contents, then you can have the 
	char* func()
if it's doing other stuff too, like validating contents against various
rules, which can result in different error codes, then you should
consider using the 
	int func(...) 
form. I think both approaches are otherwise ok.

Ahmer

-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of Robert
P. J. Day
Sent: Thursday, August 10, 2006 7:10 AM
To: C programming list
Subject: how to implement routines that return general strings?


  i'm about to design and implement a number of routines that return
strings and, as far as i can tell, i have two general design choices.

  int		func(char* buf, int bufsiz) ;
  char*		func() ;

  that first form obviously accepts the address of a buffer (and its
size) into which to copy the result, and leaves the "int" return value
to be used perhaps as an error code.  perfectly reasonable, but it
forces every invocation to first have a buffer ready to go.

  the second form would be intuitively more natural, but is affected
by the scope of the result.  the approach i would use there might be:

char*
func()
{
	static buf[128] ;	// or whatever size is appropriate
	... copy result into buf ...
	return buf ;
}

an error of some kind might be implemented by returning NULL, but that
will restrict the range of error codes i can pass back.

  are there compelling arguments either way?  while i like the natural
feel of the second form, i have to admit the first form is more
informative.  thoughts?

rday


-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2006-08-14 18:15 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-10 14:10 how to implement routines that return general strings? Robert P. J. Day
2006-08-10 17:12 ` Scott
2006-08-10 19:04   ` David Rientjes
2006-08-11 20:06   ` Kelly Burkhart
2006-08-12  9:36     ` Robert P. J. Day
2006-08-14 18:15       ` Scott
2006-08-10 21:07 ` Glynn Clements
2006-08-11  7:03   ` Raseel Bhagat
2006-08-11 18:36     ` Shorty Porty
  -- strict thread matches above, loose matches on Subject: below --
2006-08-10 16:51 Khan, Ahmer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).