当前位置:网站首页>c语言字符指针、字符串初始化问题
c语言字符指针、字符串初始化问题
2022-06-27 20:53:00 【悟空不买菜了】
之前,我在做图的存储结构的时候,用邻接矩阵来表示一张图,遇到一个段错误问题:

上面只要一输入一条边,就会出现段错误,而段错误,往往就是内存地址访问出错,比如数组越界,访问了未分配的地址,访问系统已经分配的地址,都会出错,于是我将错误定位到输入边的这个位置

上面就是我在循环之前,定义了两个字符指针,准备用来存放字符串,但是只要一scanf之后,只要访问其中某一个变量就会出现段错误,难道是说这个地址不能别访问?
于是我做了一个实验,如下操作:
直接上代码:
#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("哈哈,字符串相等\n");
}
else
{
printf("字符串不相等\n");
}
}
int main()
{
char* v1;
scanf("%s", &v1);
test(v1);
system("pause");
return 0;
}然后就会内存地址访问错误
那我又改成如下代码:
#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("哈哈,字符串相等\n");
}
else
{
printf("字符串不相等\n");
}
}
int main()
{
test("love");
system("pause");
return 0;
}运行正常:

换句话说,问题就出在:

也就是说,不能用sacnf来初始化char*,下面我们就来分析一下:

那么就是说,char* p存放的是常量字符串的地址,那我们就不能用scanf来初始化 ,因为如果用scanf来初始化,我们根本在常量区没有空间指向。而且就算有空间指向了常量区,也不能用scanf去初始化,比如如下:

因为常量区的数据读写本来就是一旦定义了就不允许改动的
那么如果我们非要用scanf来初始化一个字符串呢,那么这里我们第一时间考虑的应该是一个字符数组,代码如下:
#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("哈哈,字符串相等\n");
}
else
{
printf("字符串不相等\n");
}
}
int main()
{
char str[10] = "pxx";//OK,并且带有'\0'
scanf("%s", str);
test(str);//char*指向了一个数组空间
system("pause");
return 0;
}运行结果:

对于test来说,相当于用char* str指向了char str[10]的这片数组空间,这个时候char* str有指向了,并且可以改动,我们既可以用scanf来直接赋值。
代码改动如下:

运行结果:
边栏推荐
- Batch processing - Excel import template 1.1- support multiple sheet pages
- Detect objects and transfer images through mqtt
- [从零开始学习FPGA编程-48]:视野篇 - 智能传感器的发展与应用
- Discuz taobaoke website template / Dean taobaoke shopping style commercial version template
- 小程序referer
- 思源笔记订阅停止直接删云端数据嘛?
- vivado 如何添加时序约束
- 沉寂了一段时间 ,我又出来啦~
- EasyCVR平台路由日志功能的技术实现过程【附代码】
- Livox Lidar+海康Camera 基于loam的实时三维重建生成RGB彩色点云
猜你喜欢

Feign implements path escape through custom annotations

Discuz小鱼游戏风影传说商业GBK+UTF8版模板/DZ游戏网站模板

EXCEL 打印设置公共表头

使用SQL进行数据去重的N种方法

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

发射,接收天线方向图

Spug - 轻量级自动化运维平台

The most illusory richest man in China is even more illusory

UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation!

官宣!Apache Doris 从 Apache 孵化器毕业,正式成为 Apache 顶级项目!
随机推荐
Death of 5 yuan youkuang in Yuanqi forest
Spark bug Practice (including bug: classcastexception; connectexception; noclassdeffounderror; runtimeexceptio, etc.)
Azure Kinect DK 实现三维重建 (PC非实时版)
Using xgboost with tidymodels
跨系统数据一致性问题解决方案汇总
This year's examinees are more "desperate" than the college entrance examination
pytorch 入门指南
MySQL十八:写语句的执行过程
The choice and trade-off between vector recall and literal recall
通过tidymodels使用XGBOOST
Learn rnaseq analysis by following the archiving tutorial (I)
Detect objects and transfer images through mqtt
Sentinel
How vivado adds timing constraints
Advertising is too "wild", Yoshino "surrenders"
[can you really use es] Introduction to es Basics (II)
OData - SAP S4 OP 中使用SAP API Hub 的API
量化交易入门教程
Zabbix6.0升级指南-数据库如何同步升级?
Usage of vivado vio IP