Access returned String diffuculties

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • koshvolt
    New Member
    • Sep 2007
    • 1

    #1

    Access returned String diffuculties

    Hey guys, I've made a function to return a string of char, however when i call the function I'm only getting the 1st char back. How can i read the whole string?

    char *get_line (FILE *fp)
    {

    char *string = malloc(10 * sizeof (int));

    ->Code <-
    return string;
    }


    To access it I write
    line = get_line(pfile) ;

    Any suggestions would be helpful, ive read C is not so good at returning strings, but that's the way the function has to work eek
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    Your malloc and return of the string pointer are fine. You're making a string that can store up to 39 characters.

    If that is okay, then your problem lies with how you are filling up the string. Take a closer look at that code.

    PS: Please use [ code=c] [/code] tags around your code.

    Comment

    Working...