用字符指针中的 memmove 替换字符 [英] replacing chars with memmove in char pointer

查看:61
本文介绍了用字符指针中的 memmove 替换字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么会这样:

I am trying to understand why this works:

char myst1r[] = "hello\n";
memmove(myst1r   , myst1r + 1 , 1 );//results in "eello"

而这个:

char *mystr = "hello\n";
memmove(mystr , mystr + 1 , 1 );

导致访问冲突写入位置"错误.myst1rmystr 不是都指向 char 缓冲区的第一个成员吗?我在这里想念什么?

results in "Access violation writing location" error. Don't both myst1r and mystr point to the first member of char buffer? What do I miss here?

推荐答案

myst1r 是一个 array char ,它包含一个 其初始值设定项的副本.mystr 是指向 char指针,指向它的初始值设定项.在这两种情况下,初始值设定项都是文字字符串.区别在于可以修改数组的内容,但不能修改文字串的内容.

myst1r is an array of char that holds a copy of its initializer. mystr is a pointer to char that points to its initializer. In both cases, the initializer is a literal string. The difference is that you can modify the contents of an array, but you cannot modify the contents of a literal string.

这篇关于用字符指针中的 memmove 替换字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆