473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

return a string?

hi:
if i want a function to return me a string, how can I do it?
i know one method is by passing a string pointer to a function.
is there any implementation?

thank you very much

--
{ Kelvin@!!! }
Nov 14 '05 #1
4 1539
Try this prototype
int giveMeString( char *putMyStringHere, int lengthOfMyString);

The important thing is to have your function strictly adhere to
"lengthOfMyString".
The other way is to dynamically allocate the string in the function
itself only. But then such an approach tends to be bugy.
--
ISA

Nov 14 '05 #2
Kelvin@!!! wrote:
hi:
if i want a function to return me a string, how can I do it?
i know one method is by passing a string pointer to a function.
is there any implementation?

You don't give particularly much info..

const char * foo()
{
return "Hello";
}

/*remember to free returned string when done with it.*/
char *addhello (const char *str)
{
size_t len;
char *newstr ;
len = strlen(str)+ sizeof("Hello ");
newstr = malloc(len);
/*check for error*/
snprintf(newstr,len,"%s %s","Hello ",str);

return newstr;
}
Nov 14 '05 #3
"Kelvin@!!!" <ke*******@shaw.ca.ca> writes:
if i want a function to return me a string, how can I do it?
i know one method is by passing a string pointer to a function.
is there any implementation?


There are several approaches, none of them ideal.

You can make the caller pass in a pointer and length. This forces the
caller to guess how much space will be needed, and the function to
decide what to do if it's not enough.

You can return a pointer to a static object. This leaves the size
decision up to the caller, but can still cause problems if the result
won't fit. It also clobbers the result buffer on each call, so the
caller generally needs to copy the result to a safe location.

You can return a pointer to a result allocated by malloc(). This is
probably the most flexible method, but it requires the caller to
free() the result and risks memory leaks.

You *cannot* return a pointer to a local array variable. The array
ceases to exist when the function returns, and you're left with a
dangling pointer. The result is undefined behavior; in the worst (and
most common) case, it will appear to work properly at first, and fail
only at the most inconvenient possible moment.

I once implemented a variant of the static buffer method. The
function declared an array of half a dozen or so static buffers, with
a static index cycling through them. This allowed several results to
be used simultaneously, but the seventh call would clobber the result
from the first call. I'm not sure I'd recommend this; you have to be
as careful as with the single static buffer, but any errors won't show
up as soon.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
Keith Thompson wrote:
.... snip ...
I once implemented a variant of the static buffer method. The
function declared an array of half a dozen or so static buffers, with
a static index cycling through them. This allowed several results to
be used simultaneously, but the seventh call would clobber the result
from the first call. I'm not sure I'd recommend this; you have to be
as careful as with the single static buffer, but any errors won't show
up as soon.


Implement a circular list. At initialization time you create it as
long as you wish. From then on, any use involves:

list = list->next;
/* use list->body as needed */

and now you can bandy things about as you desire. There is only
one global pointer for list.

--
"I support the Red Sox and any team that beats the Yankees"
"Any baby snookums can be a Yankee fan, it takes real moral
fiber to be a Red Sox fan"
"I listened to Toronto come back from 3:0 in '42, I plan to
watch Boston come back from 3:0 in 04"
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
7
by: nafri | last post by:
hello all, I want to create a function that returns the first element of the Array that is input to it. However, the Input Array can be an Array of points, double, or anyother type, which means...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
2
by: utab | last post by:
Dear all, I tried sth easy(actually this was an exercise) but I tried to use the standard lib. heavily for this problem(as far as I can). There was one point I could not figure out. The problem...
6
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class...
14
by: =?Utf-8?B?QmVu?= | last post by:
Hi all, I'm trying to understand the concept of returning functions from the enclosing functions. This idea is new to me and I don't understand when and why I would need to use it. Can someone...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.