Thread: A function to return a string

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    The blind leading the blind....

    Advice from the one eyed King -

    @Earth angel. - you need to make your string declaration static like so

    Code:
    static char string[50];
    Otherwise once the functions completes, the string is no longer allocated and thus it's pointer is invalid. Sean's code is correct only because he's returning a pointer to a constant string. It won't work for variables.

    Also when you enter your while loop for the first time, you're testing it before it's been initialised. Either initialise it first, or perhaps use a do..while loop.

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Quote Originally Posted by Jez
    Code:
    static char string[50];
    Otherwise once the functions completes, the string is no longer allocated and thus it's pointer is invalid. Sean's code is correct only because he's returning a pointer to a constant string. It won't work for variables.
    I know I can't return a pointer to a local variable, but having a sting[50] constant doesn't help. Or at least I don't know how to make it helpful. I can't return a string. .....Unless I'm not understanding your advice correctly.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Jez
    The blind leading the blind....

    Quote Originally Posted by earth_angel
    The only thing is that the loop won't get out when it hits white space in the file.
    Well, since you're checking for quotations, that's no surprise. If you want to break on spaces only, you can compare against ' '. However, if you want to break on all whitespace (tabs, newlines, etc.), you can use isspace().

    Something you may want to add is a buffer length to your function; you don't want to be running off the end of your buffer. Consider the difference between gets, fgets, and why one is considered better than the other.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM