Thread: Small question about gets

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    Small question about gets

    Code:
    #include <stdio.h>
    int main()
    {
        char buffer[120]; /* Holds input and output strings */
        char *pt; /* A pointer to datatype char */
        int returnCode;
    
        while ((pt = gets(buffer)) != NULL)
        {
            returnCode = puts(buffer);
            if (returnCode == EOF) 
            {
                printf("Error on output\n");
        }
        }
    
        return 0;
    }
    Why must pt be a pointer? Also, if pt = gets(buffer) - is that checking the return value of gets, or would that return what they actually typed?

    Thanks

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Don't ever use gets, ever.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Remember that FAQ I suggested you read in its entirety? gets is included.

    Use fgets instead. Anyway, fgets, like gets which you will never use from now on, also returns a pointer, and we check if it's NULL or not. NULL is returned if end-of-file occurs before any data has been read. You don't really need to declare a pointer just to have it in the if statement like you had above:
    Code:
    if (fgets(buffer, sizeof buffer, stdin) == NULL)
    is fine.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    cwr: Don't ever use gets, ever.

    Yes, sir Hey, your the programming bada$$, not me....I swear I did read that fact, I just forgot! Thanks for the info though

  4. #4
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >Why must pt be a pointer?
    Because gets returns a pointer to the first element of the buffer that you pass to it as an argument. It makes for more convenient code. You can do this if you really want to:
    Code:
    puts ( gets ( buffer ) );
    Instead of
    Code:
    gets ( buffer );
    puts ( buffer );
    Or, for a more practical reason since gets can return NULL if the input fails in some way:
    Code:
    if ( gets ( buffer ) != NULL ) {
      /* Use buffer */
    }
    else {
      perror ( "Input error" );
      bail();
    }
    Also, if pt = gets(buffer) - is that checking the return value of gets, or would that return what they actually typed?
    Both and neither...sort of. If gets fails then it returns NULL, so pt might not be what the user typed. On the other hand, if successful, gets will return a pointer to the buffer, so pt will point to what the user typed.

    Of course, you shouldn't use gets because there's no way to make it safe from buffer overflow errors. However, your question is also relevant to fgets, which is the safe alternative to gets.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. Hi all, I have a small question...
    By Pandora in forum Windows Programming
    Replies: 3
    Last Post: 03-16-2003, 06:21 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. A small Question
    By CodeJerk in forum C++ Programming
    Replies: 2
    Last Post: 11-20-2002, 09:08 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM