#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0

    How to return string from function to main?


    Okay, I'm finally getting C (NOT C++) when BAM! Brick wall.

    I'm trying what I thought was an EASY task, but 4 hours and 4 books and vast search of internet later, I can't do something as simple as prompt for a line of text in a function, get a string of text in that function and return the string of text back to the main function.

    I have a function called getText. It has no input type, but it does have a return type of char.

    All it does is display a prompt for the user to "Enter you sentence: ", use gets to get user text, and I want to return what they type in back to main for further processing. Should be a Piece of cake

    If I didn't need to return the input to main(), I could get a line of text this way:

    #include <stdio.h>
    void getText();

    int main()
    {
    getText();
    }

    void getText()
    {
    char *name;
    printf("What is your name? ");
    scanf("%s",&name);
    printf("\nHello %s\n",&name);
    }


    But I DO need name to be returned to main(), and when I try to return name, I get problems.

    I'm sure it has something to do with pointers (since I'm just learning that), so can someone show me how to do this simple thing?

    Help!
    John
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    31
    Rep Power
    11
    try this:

    main(){

    char* userName;

    /** fill in how large ur user name can be to the malloc **/
    /** must do this to prevent Segmentation fault **/
    userName=(char*) malloc(MAXUSERNAMESIZE);

    getText(userName);
    /** now userName will contain the user name **/
    }


    void getText(char* namePtr){
    printf("What is your name? ");
    scanf("%s",&namePtr);
    printf("\nHello %s\n",&name);

    }

    Explanation:
    use of a ptr (userName) defined in main that references to mem allocation. In the getText(), what it does is that scanf will populate to the mem allocation in namePtr which is referencing to the same mem referenced by userName.
    I think this is called referenced Argument passing or something...

    hope this helps.... :)
    Sorrie if i misunderstood ur problem
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0

    Hey, that's close, but...


    How do I now return what they type in back to the main function?
    Thanks!
    John
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,473
    Rep Power
    15
    All local variables(i.e. declared in the function) are destroyed when a function returns. So, declaring the variable in the function is not what you want to do. As siddy showed you, if you declare the variable in main(), and pass it to the function, you can read some data into that variable.

    What variable do you have to return? Well that's the neat trick about pointers. You don't have to return any variable--the variable you declared in main() will magically contain the input. Just declare the return type of your function as void.
    Last edited by 7stud; August 11th, 2003 at 02:48 AM.

IMN logo majestic logo threadwatch logo seochat tools logo