Principles and return values of the get () function in C language, and get return values

Source: Internet
Author: User

Principles and return values of the get () function in C language, and get return values

The first thing to remember is Never use gets ().

This is because the gets () function does not check whether the target array can accommodate the input. to read a string into the program, the first thing to do is reserve space for storing the string. Therefore, it is easy to cause the allocated space to be too large and the array is out of bounds. However, the gets () function does not check this aspect, so the result is that the program is prone to vulnerabilities, the principle of the famous "worm" virus is to overwrite the original data with a long amount of data, leading to a crash. So for important programming, never use gets ()!

1. The gets () parameter is an address. To put the value entered from the keyboard in a certain memory, you need to specify its address, gets (array name) is usually used to pass the input string into the given array. Note: The size of this array must be defined in advance! If you do not define the size of the array, you may not know the memory to which the string is entered during input, which may overwrite the original code in the memory.

2. The first usage of gets (): directly use gets (array's name); in this way, because you do not know when it will end with the string, so every time you type '\ n', The gets () function will automatically read all the content before the line break and add' \ 0' at the end ', in addition, the string is directly returned to the program that calls it, and then gets () is read again and '\ n' read is discarded, in this way, the next read will start on a new row.

Example 1:

        #include
      #include 
        #include 
        #define MAX 81
        int main(void)
        {
            char name[MAX];
            printf("Hi, what's your name?\n");
            gets(name);
            printf("Nice name, %s\n", name);
            return 0;
        }

        /*
            Hi, what's your name?
            Herry potter
            Nice name, Herry potter

        */

3. Prototype of gets (): char * gets (char * s)

{

...

Return s;

}

Therefore, we can see that gets () returns a pointer to the char type data, and this pointer is the same as the pointer passed to it. Therefore, there are the following programs:

Example 2:

#include
         #include
         #define MAX 81
         int main (void)
         {
             char name [MAX];
             char * ptr;

             printf ("Hi, what's your name? \ n");
             ptr = gets (name); // Here ptr is a char type pointer, and at this time ptr points to the first address of name.
             printf ("% s? Ah?% s!", name, ptr); // At this time, the values pointed to by name and ptr are output. It can be found that their output is the same.
             return 0;
         }

         / *
             Hi, what's your name?
             Herry
             Herry? Ah? Herry!
         * / 
 

4. Actually, gets () has two possible return value types:

1) when the program normally inputs a string: return the address of the string to be read, that is, the first address of the array where the string is stored;

2) When the program encounters an error or the end of the file: NULL pointer is returned. Be sure not to mix the NULL pointer and NULL character ('\ 0 ');

Therefore, you can easily detect errors in the following form:

While (gets (name )! = NULL)

Note: currently, gets () is basically not used. It can be said that it is an obsolete function. Now it can be replaced by scanf (), getchar (), and fgets.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.