当前位置:网站首页>String input problem
String input problem
2022-04-23 03:22:00 【Tang Encheng_ hhhc】
First definition
char* name;
scanf("%s",name);
Although the compilation is correct , Running will crash
The second definition
char[20] name;
scanf("%s",name);
You can run it successfully
-
About why it collapsed
reason : The reason why the program crashed , Because scanf Will put the string in the memory pointed to by the pointer . But the first one doesn't initialize the pointer . The pointer just points at . Put the string in a wrong place , Of course the program crashed .
resolvent : Insert a line between two lines of code name = (char*)malloc(32*sizeof(char)) Just fine , that 32 You can change it at will , That's the number of characters you can store -
About string definition
such as :
char *s = “Hello world”
That's ok ,
Because the program will put all the string constants to be used into Static memory in . then s Point to the address of the static memory with this string .
But because it's static memory , So the content is Cannot be modified Of . If you want a string that can be modified , Then only char s[5] Or use malloc Allocate dynamic memory
After initializing with string , The position pointed to by the pointer is read-only , You write data into it , The operating system is not allowed . -
About scanf()
Respective separators :
%s: Space 、 enter 、TAB key
%d: Space 、 enter 、TAB key
%c: There is no separator , The above delimiters are treated as normal characters
in other words : If it is %s%d, The delimiter encountered when reading data will be skipped until the first non empty input is encountered , Then you can not deal with it ’\n’. -
About scanf(%s) and gets()
char string[15];
gets(string); / When a carriage return is encountered, the input is finished /
scanf("%s",string); / When a space is encountered, it is considered that the input ends /
String will be followed by ’\0’
**scanf :** When there is a carriage return , Space and tab The key will automatically add ’\0’, But enter , Space and tab The key remains in the input buffer .
**gets:** All characters entered before the Enter key are acceptable , And use ’\0’ replace ‘\n’. Enter does not stay in the input buffer
For details, please refer to the following article :
https://tieba.baidu.com/p/5079969028
版权声明
本文为[Tang Encheng_ hhhc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220621319945.html
边栏推荐
- oracle 查询外键含有逗号分隔的数据
- Flink实时数仓项目—DWS层设计与实现
- JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
- This new feature of C 11, I would like to call it the strongest!
- 一套组合拳,打造一款 IDEA 护眼方案
- 关于idea调试模式下启动特别慢的优化
- Improvement of ref and struct in C 11
- There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
- First in the binary tree
- be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
猜你喜欢
Xutils3 corrected a bug I reported. Happy
2022山东省安全员C证上岗证题库及在线模拟考试
超好用的Excel异步导出功能
This new feature of C 11, I would like to call it the strongest!
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
File upload vulnerability summary and upload labs shooting range documentary
Comprehensive calculation of employee information
QT learning summary
【无标题】
Use of ADB command [1]
随机推荐
Flink customizes the application of sink side sinkfunction
File upload vulnerability summary and upload labs shooting range documentary
2022年P气瓶充装培训试题及模拟考试
MySQL keyword group_ Concat, combined connection query
Scenario Title: how does system a use the page of system B
. net webapi access authorization mechanism and process design (header token + redis)
poi根据数据创建导出excel
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
通过 zxing 生成二维码
Why is bi so important to enterprises?
[MySQL] left Function | Right Function
Is it difficult to choose binary version control tools? After reading this article, you will find the answer
Configuration table and page information automatically generate curd operation page
Log4net is in Net core usage
12.<tag-链表和常考点综合>-lt.234-回文链表
JS inheritance
ASP. Net 6 middleware series - conditional Middleware
IDEA查看历史记录【文件历史和项目历史】
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
Optimization of especially slow startup in idea debugging mode