The principle and return value of get() function in C language

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

this is because the gets() function doesn’t check whether the target array holds input, and the first thing you need to do to read a string into your program is to reserve space for the string. The gets() function doesn’t check this aspect, so the result is that the program is vulnerable to bugs. The famous’ worm ‘virus works by overwriting data with too much data and causing it to crash. So for important programming, never use gets()!

1, gets() takes an address, which you need to specify to put the input from the keyboard into memory, and gets(array name) is typically used to pass the input string into a given array. Note: The size of the array must be defined in advance! If you don’t define the size of the array, you may not know into which memory the string was entered, which may result in overwriting the original code in that memory.

2, gets();
2, gets(); This way, because do not know when will to the end of the string, so whenever type ‘\ n’, gets () function will automatically read in front of the line breaks all the content and add ‘\ 0’ at the end, and directly put the string returned to the calling its program, and then gets () to read and be read to the ‘\ n’ discarded, so that the next read will begin in a new line.

case 1:

        #include <stdio.h>
        #include <stdlib.h>
        #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

        */

Char * gets(char * s)
3, gets()

{

return s;

}

so you can see that gets() returns a pointer to char type data with the same pointer passed to it. Therefore, there are the following procedures:

example 2:

        #include <stdio.h>
        #include <stdlib.h>
        #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 pointer to a type char, in this case ptr points to the first address of name.
            printf("%s?Ah?%s!", name, ptr); // At this point, the values pointed to by name and ptr are output, and it can be seen that they both give the same output.
            return 0;
        }

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

4. Actually gets() has two possible return value types:
1) when the program is normal input string: return to read the address of the string, also is the first of an array of strings stored address; 2) when the program errors or meet the end of file: return NULL pointer NULL, be careful not to confuse the NULL pointer and the NULL character (‘ \ 0 ‘);

so can easily detect errors in the following form:

the while (gets (name)! = NULL)

note: you basically don’t use gets(), which is arguably a defunct function, and you can now replace it with scanf(), getchar(), fgets().


Read More: