function returning a string?

Discussion in 'C Programming' started by alternativa, Jan 4, 2006.

  1. alternativa

    alternativa Guest

    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 :(
    alternativa, Jan 4, 2006
    #1
    1. Advertising

  2. alternativa <> wrote:

    > 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 */
    }

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jan 4, 2006
    #2
    1. Advertising

  3. alternativa

    M.B Guest

    alternativa wrote:
    > 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*/)
    M.B, Jan 4, 2006
    #3
  4. alternativa

    alternativa Guest

    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 :(
    alternativa, Jan 4, 2006
    #4
  5. alternativa

    Alastair Guest

    alternativa wrote:
    > 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
    Alastair, Jan 4, 2006
    #5
  6. alternativa

    Chuck F. Guest

    alternativa wrote:
    >
    > 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/>
    Chuck F., Jan 4, 2006
    #6
  7. alternativa

    Rob Adams Guest

    Rob Adams, Jan 4, 2006
    #7
  8. alternativa

    M.B Guest

    alternativa wrote:
    > 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
    M.B, Jan 5, 2006
    #8
  9. "alternativa" <> writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Jan 5, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    662
    Christos Georgiou
    May 2, 2006
  2. Protoman
    Replies:
    14
    Views:
    566
    Protoman
    Dec 11, 2005
  3. Mark Piffer
    Replies:
    9
    Views:
    907
    luserXtrog
    May 15, 2009
  4. pembed2012
    Replies:
    1
    Views:
    423
    James Kuyper
    Feb 27, 2012
  5. Haochen Xie
    Replies:
    4
    Views:
    242
    Haochen Xie
    Mar 17, 2013
Loading...

Share This Page