当前位置:网站首页>C language character pointer and string initialization
C language character pointer and string initialization
2022-06-27 23:35:00 【Wukong stopped buying vegetables】
Before , When I was working on the storage structure of the graph , Use adjacency matrix to represent a graph , Encountered a segment error problem :

Just input one edge above , There will be an error , And segment error , It is often the memory address access error , For example, the array is out of bounds , Accessed unassigned address , Access the address assigned by the system , They all make mistakes , So I located the error at the input side

That's what I did before the loop , Defines two character pointers , Ready to store strings , But just one scanf after , As long as one of these variables is accessed, a segment error will occur , This address cannot be accessed ?
So I did an experiment , Do the following :
Go straight to the code :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test(char* str)
{
printf("str=%s\n", str);
if (strcmp("love", str) == 0) {
printf(" ha-ha , String equality \n");
}
else
{
printf(" Strings are not equal \n");
}
}
int main()
{
char* v1;
scanf("%s", &v1);
test(v1);
system("pause");
return 0;
}Then memory address access error will occur
Then I changed it to the following code :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test(char* str)
{
printf("str=%s\n", str);
if (strcmp("love", str) == 0) {
printf(" ha-ha , String equality \n");
}
else
{
printf(" Strings are not equal \n");
}
}
int main()
{
test("love");
system("pause");
return 0;
}Running normally :

let me put it another way , The problem is :

in other words , Out-of-service sacnf To initialize the char*, Let's analyze :

So that's to say ,char* p The address of the constant string is stored , Then we can't use scanf To initialize the , Because if you use scanf To initialize the , We don't have a spatial point at all in the constant region . And even if there is a space pointing to the constant region , You can't use scanf To initialize , Such as the following :

Because the data read / write of the constant area is not allowed to be changed once it is defined
So if we have to use scanf To initialize a string , The first thing we should consider here is an array of characters , The code is as follows :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test(char* str)
{
printf("str=%s\n", str);
if (strcmp("love", str) == 0) {
printf(" ha-ha , String equality \n");
}
else
{
printf(" Strings are not equal \n");
}
}
int main()
{
char str[10] = "pxx";//OK, And with '\0'
scanf("%s", str);
test(str);//char* Points to an array space
system("pause");
return 0;
}Running results :

about test Come on , Equivalent to using char* str Yes char str[10] The array space of , This is the time char* str There's a point , And can be changed , We can use scanf To assign values directly .
The code changes as follows :

Running results :
边栏推荐
- [tinyriscv verilator] branch transplanted to Da Vinci development board of punctual atom
- MySQL十八:写语句的执行过程
- [js]var, let,const 的区别
- 最新云开发微信余额充电器特效小程序源码
- Working at home is more tiring than going to work at the company?
- Introduction to quantitative trading
- Applet referer
- seata
- Use of go log package log
- 【AI应用】NVIDIA GeForce RTX 1080Ti的详情参数
猜你喜欢

通过 MQTT 检测对象和传输图像

【Try to Hack】veil-evasion免杀

量化交易入门教程

SQL Server 2016详细安装教程(附注册码和资源)

跨系统数据一致性问题解决方案汇总

Feign implements path escape through custom annotations

【tinyriscv verilator】分支移植到正点原子达芬奇开发板

Excel print settings public header

MySQL十八:写语句的执行过程

Spark bug Practice (including bug: classcastexception; connectexception; noclassdeffounderror; runtimeexceptio, etc.)
随机推荐
【剑指Offer】47. 礼物的最大价值
NDSS 2022 接收的列表
上手了一个自然语言模型BLOOM
电子科大(申恒涛团队)&京东AI(梅涛团队)提出用于视频问答的结构化双流注意网络,性能SOTA!优于基于双视频表示的方法!
【PCL自学:Segmentation4】基于Min-Cut点云分割
在线JSON转PlainText工具
Advertising is too "wild", Yoshino "surrenders"
MapReduce初级编程实践
刚开始看英文文献,想问一下各位,最初应该怎么看进去?
Discuz小鱼游戏风影传说商业GBK+UTF8版模板/DZ游戏网站模板
[js]var, let, const
发射,接收天线方向图
CUDA error:out of memory caused by insufficient video memory of 6G graphics card
企业架构师面试的100个问题
Swing UI container (I)
clickonce 部署ClickOnce应用程序时出错-清单中的引用与下载的程序集的标识不匹配
良/恶性乳腺肿瘤预测(逻辑回归分类器)
【IDEA】IDEA 格式化 代码技巧 idea 格式化 会加 <p> 标签
ICML 2022:ufrgs | optimistic linear support and subsequent features as the basis for optimal strategy transfer
[js]var, let,const 的区别