Preferred way to pass a string

Reply

Join Date: Oct 2006
Posts: 2,344
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 194
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Preferred way to pass a string

 
0
  #1
Sep 20th, 2008
What's the preferred way to pass a string to and from a function?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,494
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 834
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Preferred way to pass a string

 
0
  #2
Sep 20th, 2008
The approach used by fgets() is a good way.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: plumsauce is an unknown quantity at this point 
Solved Threads: 0
plumsauce plumsauce is offline Offline
Newbie Poster

Re: Preferred way to pass a string

 
0
  #3
Sep 22nd, 2008
as a pointer

you don't have to pass it back, because the calling function already knows the pointer

of course, you may always pass back a pointer, if for example you are doing a search for a substring.
newsreports.org - 5 second website check
basicstate.com - free website uptime monitor and alert service
edgedirector.com - managed dns global loadbalance and failover service
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,441
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1599
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Preferred way to pass a string

 
0
  #4
Sep 22nd, 2008
Passing back the original pointer is sometimes also used so that the function can be a nested function call. Example:
  1. const char* foo(char *ptr)
  2. {
  3. ...
  4. <snip>
  5. return ptr;
  6. }
  7.  
  8. int main()
  9. {
  10. printf("%s\n", foo("How now brown cow."));
  11. }
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 990 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC