function returning a string?

A

alternativa

Hi,
how to write a function that would return a string?
I've tried something like this:

char enterstring(void)
{
char somestring[15];
printf("\tstring: ");
fgets(somestring, sizeof somestring, stdin);
return somestring;
};

but it doesn't work :(
 
C

Christopher Benson-Manica

alternativa said:
how to write a function that would return a string?

You either need to pass it a place to store the string you want to
return or malloc() space on your own, keeping in mind that it is the
caller's responsibility to see that the returned string is free()'d
properly.
I've tried something like this:
char enterstring(void)

somestring is a character array, not a char. If you want to return a
string, return char *.
{
char somestring[15];
printf("\tstring: ");

IIRC, without a newline here, you are not guaranteed to see this
string before you are expected to enter a string. fflush() would
probably be helpful.
fgets(somestring, sizeof somestring, stdin);

At least you didn't use gets(). Good.
return somestring;

Completely wrong. The space associated with somestring will be gone
and inaccessable after the function returns.

#define MAX_STRING 128

char * enterstring( void )
{
char *ret=malloc( MAX_STRING );
if( !ret ) {
/* Error */
return ret;
}
fgets( ret, MAX_STRING, stdin );
return ret; /* Caller is responsible for free()'ing this pointer */
}
 
M

M.B

alternativa said:
Hi,
how to write a function that would return a string?
I've tried something like this:

char enterstring(void)
{
char somestring[15];
printf("\tstring: ");
fgets(somestring, sizeof somestring, stdin);
return somestring;
};

but it doesn't work :(

char *enterstring(void) /* note return typw is char *
not char*/
{
static char somestring[15]; /u cannot return ptr to a auto
local var so use static */
printf("\tstring: ");
fgets(somestring, sizeof somestring, stdin);
return somestring;
} /* u dont put ';' after
function brace */


take care to use strcpy for persistant use of string
ex int main(void) { char a[20]; strcpy(a,enterstring()); ...}

better is to pass argument of enterstring as a char *
ex:
void enterstring(char *str,int len /*to take care of max length*/)
 
A

alternativa

thanks a lot, now I have a new problem.. I wish to pass this string to
a field of a structure.
For other fields - containing numbers - I used something like this:

somenumber = enternumber ();
currp->num1 = sumenumber;

where currp->num1 is a pointer to a structure's field called 'num1'.

Unfortunately in the case of string I cannot compile and the message
is: incompatibile types in assignment. I guess I should now include the
fact that I use pointers, but everything I've tried conclued with a
message "pointer from integer without a cast".
I know I'm one of those annoying newbies ;), but please safe my life..
I have no idea how to solve this problem :(
 
A

Alastair

alternativa said:
thanks a lot, now I have a new problem.. I wish to pass this string to
a field of a structure.
For other fields - containing numbers - I used something like this:

somenumber = enternumber ();
currp->num1 = sumenumber;

where currp->num1 is a pointer to a structure's field called 'num1'.

Unfortunately in the case of string I cannot compile and the message
is: incompatibile types in assignment. I guess I should now include the
fact that I use pointers, but everything I've tried conclued with a
message "pointer from integer without a cast".
I know I'm one of those annoying newbies ;), but please safe my life..
I have no idea how to solve this problem :(

Hi Alternativa,

It sounds like you just have a string / number confusion. I think you
are trying to get a user to input a number which you read from the
command line (or whereever) as a string e.g. "52". Then you want to
store "52" as a number 52.

So a good way around this is to NOT return a string pointer (char *),
but to instead convert it to a number first and then return and
integeter.

Or alternativly convert after the function. BUT you cannot just assign
an integer to a char value and expect to get a valid result!

Can of course cast the (char *) to an integer if you want to "force"
the issue, but this will not give you the result you want (e.g.
my_int = (int) my_char_ptr;)

Alastair
 
C

Chuck F.

alternativa said:
>
thanks a lot, now I have a new problem.. I wish to pass this
string to a field of a structure. For other fields - containing
numbers - I used something like this:

Consider using my ggets function to input the string. Available
at: <http://cbfalconer.home.att.net/download/ggets.zip>

Then you can:

struct thing {
....
char *wantedstring;
....
} thething;

.....

if (0 != ggets(&thething.wantedstring)) failure();
else {
donesocarryon();
}

or, if you need to access some file f other than stdin:

if (0 != fggets(&thething.wantedstring, f)) failure();
else {
donesocarryon();
}

Note that you pass ggets the address of a pointer to char. When
done with it don't abandon it, you should free it:

free(thething.wantedstring);
thething.wantedstring = NULL;

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
M

M.B

alternativa said:
thanks a lot, now I have a new problem.. I wish to pass this string to
a field of a structure.
For other fields - containing numbers - I used something like this:

somenumber = enternumber ();
currp->num1 = sumenumber;

where currp->num1 is a pointer to a structure's field called 'num1'.

Unfortunately in the case of string I cannot compile and the message
is: incompatibile types in assignment. I guess I should now include the
fact that I use pointers, but everything I've tried conclued with a
message "pointer from integer without a cast".
I know I'm one of those annoying newbies ;), but please safe my life..
I have no idea how to solve this problem :(

Furst of all I do not know the declaration of function "enternumber ()"
I guess it is like (from past mails also)

char *enternumber(void)

And structure definition is like
struct xyz {
int *num1; /* why the hell is this a pointer, i dont understand
*/
...
};

Your code may be
struct xyz *currp=calloc(...);
currp0>num1=calloc(...); /* this is needed if num1 is int * */

then *(currp->num1)=atoi(enternumber());
should work fine if @stdlib.h is included


-
M.B
 
K

Keith Thompson

alternativa said:
how to write a function that would return a string?
I've tried something like this:

char enterstring(void)
{
char somestring[15];
printf("\tstring: ");
fgets(somestring, sizeof somestring, stdin);
return somestring;
};

but it doesn't work :(

The C FAQ is at <http://www.c-faq.com>. See questions 7.5a and 7.5b.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top