关于字符串常量的问题,第5.5段K& R. [英] Question about string constants, para 5.5 K&R

查看:51
本文介绍了关于字符串常量的问题,第5.5段K& R.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从书中

"这些定义之间有一个重要的区别:


char amessage [] =" now is the time" ;;

char * pmessage ="现在是时间" ;;


snip


另一方面, pmessage是一个指针,初始化为指向一个

字符串const;如果你试图修改字符串常量,指针可能会被修改为指向别处,但

结果是未定义的。


1)所以声明

* pmessage =''b'';

可能不会改变''now''变为''bow''??????

但声明

amessage [0] =''b'';


始终保证将''now''更改为''' bow''


请注意,文中没有提到const限定符。我理解amessage是一个指向char的常量指针,但实际上并不是
理解为什么pmessage指向一个常量char,特别是如果我

决定使用pmessage作为自动变量(暗示在
RAM(读/写存储)中的堆栈将保持现在是时间

From the book
"There is an important difference between these definitions:

char amessage[]="now is the time";
char *pmessage ="now is the time";

snip

On the other hand, pmessage is a pointer, initialized to point a
string const; the pointer may be modified to point elsewhere, but the
result is undefined if you try to modify the string constants."

1) So the statement
*pmessage=''b'';
may not change ''now'' to ''bow''??????
but the statement
amessage[0]=''b'';

is always guaranteed to change ''now'' to ''bow''

Please note there is no mention of the const qualifier in the text. I
understand amessage is a constant pointer to a char, but really don''t
understand why pmessage is pointer to a constant char, especially if I
decide to use pmessage as an auto variable (implying that the stack in
RAM (read/write storage) will hold "now is the time"

推荐答案

nospam写道:
nospam wrote:

从书中

"有一个重要的区别在这些定义之间:


char amessage [] =" now is time" ;;

char * pmessage =" now is time"; < br $>
snip


另一方面,pmessage是一个指针,初始化为指向一个

字符串const;如果你试图修改字符串常量,指针可能被修改为指向别处,但

结果是未定义的。


1)所以语句

* pmessage =''b '';

可能不会改变''now'''''bow''??????
From the book
"There is an important difference between these definitions:

char amessage[]="now is the time";
char *pmessage ="now is the time";

snip

On the other hand, pmessage is a pointer, initialized to point a
string const; the pointer may be modified to point elsewhere, but the
result is undefined if you try to modify the string constants."

1) So the statement
*pmessage=''b'';
may not change ''now'' to ''bow''??????



或者它可能会使你的厕所爆炸,这是未定义的行为。

Or it might make your toilet explode, it is undefined behaviour.


但声明

amessage [0] =''b'';


始终保证将''now''更改为''bow''
but the statement
amessage[0]=''b'';

is always guaranteed to change ''now'' to ''bow''



是的,因为amessage是一个(零终止)自动char数组。

Yes, because amessage is a (zero terminated) automatic array of char.


请注意,没有提及文本中的const限定符。我理解amessage是一个指向char的常量指针,但实际上并不是
理解为什么pmessage指向一个常量char,特别是如果我

决定使用pmessage作为自动变量(暗示在
RAM(读/写存储)中的堆栈将保持现在是时间
Please note there is no mention of the const qualifier in the text. I
understand amessage is a constant pointer to a char, but really don''t
understand why pmessage is pointer to a constant char, especially if I
decide to use pmessage as an auto variable (implying that the stack in
RAM (read/write storage) will hold "now is the time"



pmesssage可能是一个自动变量,但它指的是一个

字符串文字,其位置是一个实现细节。


不幸的是,C99委员会错过了使用字符串文字修复char * *
char *的机会,不像C ++委员会那样。


-

Ian Collins。

pmesssage may be an automatic variable, but what it points to is a
string literal, the location of which is an implementation detail.

Unfortunately, the C99 committee missed the opportunity to fix the use
of char* with string literals, unlike the C++ committee who did.

--
Ian Collins.


nospam说:


< snip>
nospam said:

<snip>

请注意文本中没有提到const限定符。我

理解amessage是指向char的常量指针,但是真的不要'

理解为什么pmessage是指向常量char的指针,特别是如果我确定
决定使用pmessage作为自动变量(意味着堆栈在

RAM中(读取) / write storage)将保持now is the time。
Please note there is no mention of the const qualifier in the text. I
understand amessage is a constant pointer to a char, but really don''t
understand why pmessage is pointer to a constant char, especially if I
decide to use pmessage as an auto variable (implying that the stack in
RAM (read/write storage) will hold "now is the time"



谁说它必须是RAM?允许实现将字符串

文字放入只读存储器(ROM)中。他们可能会这样做,而且他们可能不会因为实施而无法判断。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Who says it has to be RAM? Implementations are allowed to put string
literals in read-only memory (ROM). They might do that, and they might not
- you never can tell, with implementations.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.





1月28日,02:30,Ian Collins< ian-n ... @ hotmail.comwrote:


On 28 Jan, 02:30, Ian Collins <ian-n...@hotmail.comwrote:

nospam写道:
nospam wrote:

从书中

"这些定义之间存在重要差异:
From the book
"There is an important difference between these definitions:


char amessage [] =" now is time" ;;

char * pmessage =" now is the time" ;;
char amessage[]="now is the time";
char *pmessage ="now is the time";


snip
snip


另一方面,pmessage是指针,初始化为指向一个

字符串const;可以修改指针以指向其他位置,但如果您尝试修改字符串常量,则

结果是未定义的。
On the other hand, pmessage is a pointer, initialized to point a
string const; the pointer may be modified to point elsewhere, but the
result is undefined if you try to modify the string constants."


1)所以语句

* pmessage =''b'';

可能不会改变''now''到''bow''??????
1) So the statement
*pmessage=''b'';
may not change ''now'' to ''bow''??????


或者它可能会使您的厕所爆炸,这是未定义的行为。
Or it might make your toilet explode, it is undefined behaviour.



我已经多次提到这个问题了,我有一个

的问题。我很欣赏标准说这是未定义的

行为。所以根据标准,*可能*发生。我的

问题关于*实际*在真实系统上发生的事情。


人们说字符串可能会被放入ROM中,所以字符串

实际上并没有改变。很公平,但可能在PC上不会发生b $ b。大多数人在询问这个问题时似乎报告的症状是他们的系统段错误。为什么/这可能会发生?b $ b?标准要求字符串可以正确读取,所以

为什么当你尝试写它时,事情会出现严重错误?当然,如果一台PC可以找到一个可以从中读取的内存地址,它可以写入它吗?

I''ve seen this problem raised a number of times, and I have a
question. I appreciate that the standard says that this is undefined
behaviour. So according to the standard anything *could* happen. My
question regards what *actually* happens on real systems.

People have said that the string may be put into ROM, so the string
doesn''t actually get changed. Fair enough, but presumably it tends not
to happen on a PC. The symptom most people seem to report when asking
about this problem is that their system segfaults. Why does/might this
happen? The standard requires that the string be readable properly, so
why do things go horribly wrong when you try to write to it? Surely if
a PC can find a memory address to read from it it can write to it?


这篇关于关于字符串常量的问题,第5.5段K&amp; R.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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