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:
- C language: Implementation of dynamic array initialization, insertion, traversal, deletion and destruction
- Spring boot uses thread pool to realize asynchronous processing without return and asynchronous processing with return
- [Solved] AndroidStudio libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined referen
- OpenFeignClient Use Object to Receive text/plain Type Return Error
- How to get the current time in java time string
- How to Open Files by Dragging and Dropping in C#
- [PROJECT] itdage java to get the weather and send text messages
- [Solved] Mybatis Error: attempting to get column ‘XX’ from result set
- OTA Pack Compile Error: ExternalError: Failed to run signapk.jar: return code 1:Error: A JNI error has occurred
- [Solved] SpringBoot Project Startup Error: Field userMapper in com.demo.controller.MemberController required a bean of type ‘c
- Client Error: Could not get a resource from the pool [How to Solve]
- [Solved] Error attempting to get column ‘xxxx_time‘ from result set. Cause: java.sql.SQLFeatureNotSupportedEx
- C++ String case conversion and transform, tower, upper, usage
- Java callback function implementation case
- C++: Implementation of multi-channel IO transfer with select
- Problems and causes of Java’s main function format (public static void main (string args()))
- Jjwt error: ERROR 9856 — [nio-8083-exec-2] o.a.c.c. [. [. [. [/]. [dispatch server]
- Consumer service instance error: HTTP get http://xxx/actuator/health: 503 output: {“status”: “out_of_service”
- JAVA Error: Error attempting to get column ‘XXX’ from result set. Cause java.sql.