当前位置:网站首页>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
边栏推荐
- Super easy to use [general excel import function]
- 【VS Code】解决jupyter文件在vs code中显示异常的问题
- Optimization of especially slow startup in idea debugging mode
- Batch download of files ---- compressed and then downloaded
- 数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
- Top ten project management software similar to JIRA
- JS, bind the event for a label with input, and then bind the stand-alone event in the parent element. The event is executed twice and solved
- After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
- Can you answer the questions that cannot be answered with a monthly salary of 10k-20k?
- Mysql database, inconsistent index character set, slow SQL query, interface timeout
猜你喜欢

Blazor University (12) - component lifecycle

It can receive multiple data type parameters - variable parameters

软件测试相关知识~

IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
![Eight elder brothers chronicle [4]](/img/87/f695d0275f8a66b9def48a75668d15.png)
Eight elder brothers chronicle [4]

《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
![Idea view history [file history and project history]](/img/b2/3128105eca7449c55146ce3b9e5c2e.png)
Idea view history [file history and project history]

xutils3修改了我提报的一个bug,开心

全新的ORM框架——BeetlSQL介绍

“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
随机推荐
Téléchargement en vrac de fichiers - téléchargement après compression
二进制文件版本控制工具选择难?看完这篇你会找到答案
批量下载文件----压缩后再下载
2022a special equipment related management (elevator) work license question bank and simulation examination
Idea view history [file history and project history]
Docker拉取mysql并连接
Fight leetcode again (290. Word law)
2022年P气瓶充装培训试题及模拟考试
2022g2 boiler stoker examination question bank and online simulation examination
Yes Redis using distributed cache in NE6 webapi
Detailed explanation of socket programming send() and recv() functions
Top 9 task management system in 2022
EasyUI's combobox implements three-level query
手机连接电脑后,QT的QDIR怎么读取手机文件路径
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
Experiment 5 components and event handling
Supersocket is Use in net5 - startup
AWS from entry to actual combat: creating accounts
Build websocket server in. Net5 webapi