char* s = "hello!";*(s-1) = '\0';这样用有错吗?

江南烟雨梦 2009-12-02 06:10:01
#include <windows.h>
#include <stdio.h>

int main()
{
char *s = "Golden Global View Global";

*(s-1) = '\0';

printf("%s\n",s);

system("pause");

return 0 ;
}
程序出错了,在*(s-1) = '\0';处中断.
为什么啊?
...全文
216 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
adamchao 2009-12-05
  • 打赏
  • 举报
回复
char *s = "Golden Global View Global";
char s[] = "Golden Global View Global";
的区别:
第一个相当于:const char *s = "Golden Global View Global";指向字符常量的指针是不能修改的,第二个在堆栈里,是可以修改的。
codelabs 2009-12-05
  • 打赏
  • 举报
回复
s是字符串是首地址
s-1就是字符串首地址的前一个位置,越界。
你的意思是不是想在字符串的最后面加一个 '\0'
如果是这样的话,没必要了,这是系统自动加的
stjay 2009-12-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 beyond0824 的回复:]
引用 4 楼 hildalh 的回复:
"Golden Global View Global"

是存储在静态存储区的,*(s-1) = '\0' 不行,同样 *s = '\0'也会出问题


char *s = "Golden Global View Global";
如果这样定义,要修改s中某一个字符的内容,该怎么做呢?
[/Quote]

Windows下,将该内存修改成可写咯
VirtualProtect(s,strlen(s),PAGE_READWRITE,&OldProtect);
s[0]='a';
*(s+1)='b';

VirtualProtect(s,strlen(s),OldProtect,&OldProtect);
rayhome1987 2009-12-02
  • 打赏
  • 举报
回复
C专家编程,有说到这一点。
char *s 是一个申明,char s[]是一个定义。具体你可以去仔细看看书(推荐)。还有很多诸如此类的容易混淆的概念。

漫跑者也 2009-12-02
  • 打赏
  • 举报
回复
指针s指向字符串头,s-1尚未定义,没有意义。
江南烟雨梦 2009-12-02
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hildalh 的回复:]
引用 7 楼 beyond0824 的回复:
引用 4 楼 hildalh 的回复:
"Golden Global View Global"

是存储在静态存储区的,*(s-1) = '\0' 不行,同样 *s = '\0'也会出问题


char *s = "Golden Global View Global";
如果这样定义,要修改s中某一个字符的内容,该怎么做呢?


char s[] = "hello...";

这样就可以修改了呀
[/Quote]
char *s = "Golden Global View Global";
这样不能改噢!
linuhuge 2009-12-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 beyond0824 的回复:]
引用 4 楼 hildalh 的回复:
"Golden Global View Global"

是存储在静态存储区的,*(s-1) = '\0' 不行,同样 *s = '\0'也会出问题


char *s = "Golden Global View Global";
如果这样定义,要修改s中某一个字符的内容,该怎么做呢?
[/Quote]

char s[] = "hello...";

这样就可以修改了呀
varding 2009-12-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 beyond0824 的回复:]
char *s = "Golden Global View Global";
如果这样定义,要修改s中某一个字符的内容,该怎么做呢?
[/Quote]

你这样定义的字符串是个常量放在了代码段,不能去改变它

只能

char s[] = "Golden Global View Global"; 
江南烟雨梦 2009-12-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hildalh 的回复:]
"Golden Global View Global"

是存储在静态存储区的,*(s-1) = '\0' 不行,同样 *s = '\0'也会出问题
[/Quote]

char *s = "Golden Global View Global";
如果这样定义,要修改s中某一个字符的内容,该怎么做呢?
varding 2009-12-02
  • 打赏
  • 举报
回复
char *s = "Golden Global View Global"; 
这个字符串是放在代码段中的,不能改变字符串的内容


char s[] = "Golden Global View Global"; 
是放在堆栈中的,可以随意改变字符串内容,但是要注意不要越界,你的*(s-1)越界了
linuhuge 2009-12-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 beyond0824 的回复:]
引用 2 楼 varding 的回复:
char *s = "Golden Global View Global"; 是放在代码段中的,不可改变 

可以这样:

char s[] = "Golden Global View Global";


*(s + 1)='\0';
s[1]='\0';


*(s-1) = '\0'; 为什么要这样弄?越界了吧?

char *s = "Golden Global View Global";
char s[] = "Golden Global View Global";
这两个有什么区别吗?
[/Quote]

char s[] = "hello";

这种方式呢,只不过是做了拷贝,所以可以
linuhuge 2009-12-02
  • 打赏
  • 举报
回复
"Golden Global View Global"

是存储在静态存储区的,*(s-1) = '\0' 不行,同样 *s = '\0'也会出问题
江南烟雨梦 2009-12-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 varding 的回复:]
char *s = "Golden Global View Global"; 是放在代码段中的,不可改变 

可以这样:

char s[] = "Golden Global View Global";


*(s + 1)='\0';
s[1]='\0';


*(s-1) = '\0'; 为什么要这样弄?越界了吧?
[/Quote]
char *s = "Golden Global View Global";
char s[] = "Golden Global View Global";
这两个有什么区别吗?
varding 2009-12-02
  • 打赏
  • 举报
回复
char *s = "Golden Global View Global"; 是放在代码段中的,不可改变

可以这样:

char s[] = "Golden Global View Global";


*(s + 1)='\0';
s[1]='\0';


*(s-1) = '\0'; 为什么要这样弄?越界了吧?
jenf 2009-12-02
  • 打赏
  • 举报
回复
s-1指向了字符串第一个字符的前一位置,给这个指针通过间接引用赋值是未定义的。

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧