这有什么问题:* p ++ = * s ++ [英] What is wrong with this: *p++=*s++

查看:82
本文介绍了这有什么问题:* p ++ = * s ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在这里有一个问题:


如果我声明:

char * p =" Hello" ;;


char * s =" world"


我说


* p ++ = * s ++;


我遇到分段错误。但是当我使用数组(比如p [80],s [80])

而不是指针* p和* s时,每件事情都可以。

有人能告诉我这有什么问题?

问候,

马特

解决方案

ma ******** @ hotmail.com (马特)写在

新闻:f6 ************************@posting.google.com:

我在这里有一个问题:

如果我声明:

char * p =" Hello" ;;

char * s =" world"

我说

* p ++ = * s ++;

我遇到了分段错误。但是当我使用数组(比如p [80],s [80])
而不是指针* p和* s时,每件事都没问题。




当然。 p和s指向不可修改的内存。在嵌入式系统上,这些* constant *字符串可能在FLASH内存中。
很明显,你不希望
期望能够通过简单的写入来修改FLASH内存吗?不,也许你可以尝试在任何系统上修改字符串文字。你应该学会

正确定义p和s:


const char * p =" Hello" ;;

const char * s =" World";


然后你会得到正确的编译时错误而不是运行时

错误。


-

- 马克 - >

-


马特写道:



我在这里有一个问题:

如果我声明:

char * p =" ;你好" ;;

char * s =" world"

我说

* p ++ = * s ++;
<我得到了一个分段错误。但是当我使用数组(比如p [80],s [80])
而不是指针* p和* s时,每件事情都可以。
有人可以告诉我这里有什么问题吗?



这是comp.lang.c中的问题16.6。经常

问题(FAQ)列表

http://www.eskimo.com/~scs/C-faq /top.html


-
呃*********@sun.com


" Eric Sosman" <尔********* @ sun.com>在消息新闻中写道:40 ************** @ sun.com ...

Matt写道:


如果我宣布:

char * p =" Hello" ;;

char * s =world

我说

* p ++ = * s ++;

我遇到了分段错误。但是当我使用数组(比如p [80],s [80])
而不是指针* p和* s时,每件事情都可以。
有人可以告诉我这里有什么问题吗?这是comp.lang.c中的问题16.6



正如16.6所述,它在1.32中更早被覆盖。

常见问答(FAQ)列表

http://www.eskimo.com/~scs/C-faq/top.html




-

彼得


Hi,
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?
Regards,
Matt

解决方案

ma********@hotmail.com (Matt) wrote in
news:f6************************@posting.google.com :

I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.



Of course. p and s point to non-modifiable memory. On embedded systems
these *constant* strings might be in FLASH memory. Clearly you don''t
expect to be able to modify FLASH memory with a simple write? No, nor may
you attempt to modify string literals on any system. You should learn to
define p and s correctly as:

const char *p = "Hello";
const char *s = "World";

Then you will get the correct compile time error instead of a run-time
error.

--
- Mark ->
--


Matt wrote:

Hi,
I have a probelm here:

If I declare:
char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?



This is Question 16.6 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun.com


"Eric Sosman" <Er*********@sun.com> wrote in message news:40**************@sun.com...

Matt wrote:

Hi,

I have a probelm here:

If I declare:

char *p="Hello";

char *s="world"

and I say

*p++=*s++;

I get a segmentation fault. But when I use arrays( say p[80], s[80])
instead of pointers *p and *s every thing is fine.
Can someone tell me what is wrong here?
This is Question 16.6 in the comp.lang.c



As that 16.6 states, it''s covered even earlier in 1.32.
Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html



--
Peter


这篇关于这有什么问题:* p ++ = * s ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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