Results 1 to 8 of 8

Thread: gets() does not append '\0'?

  1. #1
    Join Date
    Feb 2011
    Location
    California
    Beans
    88
    Distro
    Xubuntu 11.10 Oneiric Ocelot

    Question gets() does not append '\0'?

    Whenever I use the c function gets() to input a string, then try to parse the string with a pointer in a loop using pointer != '\0' as the loop termination condition, the pointer always goes way out of the bounds of the string anyhow.

    Code:
    #include <stdio.h>
    
    #define string_terminator    '\0'
    #define MAX_CHARS            80
    
    void main()
    {
        char input[MAX_CHARS];
        char *ptr_to_string  = input;
    
        gets ( input );
    
        for (input; input != string_terminator; input++)
            puts ( input );
    This code outputs all kinds of stuff outside of the array of chars. It does the exact same thing if I use

    Code:
    for (ptr_to_string; ptr_to_string != string_terminator; ptr_to_string++)
    What am I doing wrong?
    No living creature should be treated as if it were property.

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: gets() does not append '\0'?

    from its own manpage:

    Never use gets()

    its so bad it has even been removed from the C standard!

    use fgets instead

    as for your problem ptr_to_string != string_terminator compares a pointer and will thus always be true
    you have to dereference input to get the value it points to:
    Code:
    *ptr_to_string != string_terminator
    Last edited by MadCow108; July 17th, 2012 at 10:47 PM.

  3. #3

    Re: gets() does not append '\0'?

    The pointer `input` never becomes 0. The referent of input, i.e. the character it points to, will become 0 when input points 1 past the end of the string. You need to change the test to `*input` instead of just `input`. A C-idiomatic way to do this might be more like
    Code:
    while (*input++) {
        puts(input);
    }
    However you should really not use gets in the first place.

  4. #4
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: gets() does not append '\0'?

    Incidentally, you should probably stop using whatever learning material you are using right now which gave you the idea to use gets().
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Feb 2011
    Location
    California
    Beans
    88
    Distro
    Xubuntu 11.10 Oneiric Ocelot

    Re: gets() does not append '\0'?

    Quote Originally Posted by Bachstelze View Post
    Incidentally, you should probably stop using whatever learning material you are using right now which gave you the idea to use gets().
    I would, but the library doesn't have any other books on c available, and I don't have internet access at home. I read in the documentation of gets() that it should not be used, but I haven't learned how to manipulate streams yet so I don't know how to use fgets.
    No living creature should be treated as if it were property.

  6. #6

    Re: gets() does not append '\0'?

    It's easy.

    Code:
    fgets(buf, sizeof buf, stdin);
    Nothing magic about it, `stdin` just represents that you want to read from standard input (usually a terminal). You can replace it with other things later when you want to read from files.

    (Edit: In case it wasn't clear, `buf` here should be the array you want to read into.)
    Last edited by trent.josephsen; July 18th, 2012 at 12:08 AM.

  7. #7
    Join Date
    Aug 2005
    Location
    The Local Group
    Beans
    631

    Re: gets() does not append '\0'?

    Quote Originally Posted by trent.josephsen View Post
    Code:
    fgets(buf, sizeof buf, stdin);
    This is partially correct, but it's important to note that it will only work if buf is allocated statically; it will fail horribly if buf is allocated dynamically. This trips up beginners all the time.

    StarKid - Instead of
    Code:
    gets(buf);
    use
    Code:
    fgets(buf, buf_size, stdin);
    buf_size is however many characters the buffer can hold. (In your example, it's MAX_CHARS.) stdin is stdin; don't mess with it. Later, you'll learn about reading from files, which is what that third argument lets you do.

    Seriously, though, don't ever use gets.

  8. #8

    Re: gets() does not append '\0'?

    Quote Originally Posted by Lux Perpetua View Post
    This is partially correct, but it's important to note that it will only work if buf is allocated statically; it will fail horribly if buf is allocated dynamically. This trips up beginners all the time.
    It would be more accurate to say that it works only when buf is an array object and not when buf is a pointer object. Whether the memory itself is statically or dynamically allocated is irrelevant, as sizeof is evaluated at compile time and the first argument will decay to a pointer anyway.

    You're quite right to warn about it, though.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •