用指针引用字符串

发布于 2024-10-09 00:25:06 字数 426 浏览 5 评论 0原文

可能的重复:
为什么简单的 C 代码会收到分段错误?

为什么代码片段2 的行为与片段 1 不同吗?

//Code snippet 1
char pstr[] = "helloworld";
char *p = pstr; 
p[2] = 'd';

//Code snippet 2
char *p = "helloworld";
p[2] = 'd'; //error: access violation

PS请原谅我的无知。

Possible Duplicate:
Why does simple C code receive segmentation fault?

Why code snippet 2 doesn't behave like snippet 1?

//Code snippet 1
char pstr[] = "helloworld";
char *p = pstr; 
p[2] = 'd';

//Code snippet 2
char *p = "helloworld";
p[2] = 'd'; //error: access violation

P.S Forgive my ignorance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毅然前行 2024-10-16 00:25:06

第一个片段创建一个 char 数组并将其内容初始化为“helloworld”。然后你改变它的第三个元素。

第二个只是创建一个指向 char 的指针,该指针指向字符串文字。然后您尝试更改该文字的第三个字符。在许多现代编译器生成的代码中,字符串文字是不可写的。

编辑:

GCC 曾经有一个 -fwritable-strings 选项,使字符串文字可写,因为周围有依赖于此行为的遗留代码。该选项已在 GCC 4.0 发行版系列中删除。

The first snippet creates an array of char and initializes its contents to "helloworld". Then you change its third element.

The second one simply creates a pointer to char that points to a string literal. Then you attempt to change the third character of that literal. String literals are not writable in code produced by many modern compilers.

EDIT:

GCC used to have an -fwritable-strings option that enabled string literals to be writable, since there is legacy code around that depends on this behaviour. That option was removed in the GCC 4.0 release series.

许一世地老天荒 2024-10-16 00:25:06

"helloworld" 是一个 const char 数组。类型系统中存在一个漏洞,允许您使用 char* 指向它,因为存在大量使用 char * 指向只读数据的代码,并且这是安全的。

但是 const_cast 规则适用,即使您创建了一个指向 const 的指针,您实际上也无法写入 const 数据。

"helloworld" is an array of const char. There's a hole in the type system which allows you to point to it with a char*, because a lot of code exists which uses a char * to point to readonly data and this is safe.

But const_cast rules apply, you can't actually write to the const data even if you make a non-const pointer to it.

吖咩 2024-10-16 00:25:06

如果您能告诉我们他们的行为有何不同,将会有所帮助。

但作为猜测,我认为你的问题是第二种形式的“p”指向只读内存中的字符串。尝试通过指针“p”写入将导致程序失败。

我可以告诉你,Gnu c++ 编译器会警告你这一点。

It would help if you could tell us in what way they're behaving differently.

But as a guess, I think your problem is that the second form has 'p' pointing to a string in read-only memory. Attempts to write through the pointer 'p' would result in a program failure.

I can tell you that the Gnu c++ compiler will warn you about this.

小…楫夜泊 2024-10-16 00:25:06

我猜当你说“行为不像”时,你的意思是一个抛出非法访问异常(或类似的东西),而另一个给出编译时警告或错误?

答案是,在第一种情况下,您将创建一个指向您自己的内存的指针,并将 c9ontents 复制到其中。此时编译器会忘记它曾经是一个指向静态内存的指针;然而,运行时系统不会忘记。

在另一种情况下,编译器“知道”p 是指向静态内存的指针,因此有机会说“哇,伙计,不能这样做”。

但这只是一个猜测,不知道它到底有什么不同。它还将依赖于编译器和实现。

I'm guessing when you say "doesn't behave like" you mean one throws an illegal access exception (or something similar) while the other gives a compile time warning or error?

The answer is that in the first case, you're creating a pointer to your own memory, and copying the c9ontents into it. At that point the compiler forgets it used to be a pointer to static memory; the run time system, however, doesn't forget.

In the other case, the compiler "knows" p is a pointer to static memory, and so has the chance to say "whoa, dude, can't do that".

But this is a bit of a guess without knowing exactly what it does differently. it's also going to be compiler and implementation dependent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文