为什么char * p =“不能修饰”; char p [] =“可以修改” [英] why char *p = "can not modiy"; char p[] = "can modify"

查看:89
本文介绍了为什么char * p =“不能修饰”; char p [] =“可以修改”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定义时

char * p ="不能修改;


p [0] =''b'';是不允许的,


但是如果你声明p为

char p [] ="可以修改" ;;


p [0] =''b'';好吗?

为什么?

两个声明+定义之间有什么区别?

when define
char *p = " can not modify";

p[0] =''b'' ;is not allowed,

but if you declare p as
char p[] = "can modify";

p[0] = ''b''; is ok?
why?
what''s difference between the two declaration + definition?

推荐答案

ba*********@gmail.com 写道:
定义时
char * p ="不能修改;


这只定义了一个初始化的指针

指向一个文字字符串,即一个驻留在内存中的字符串

你不允许修改。

p [0] =''b'';不允许,
但如果你宣布p为
char p [] ="可以修改" ;;
when define
char *p = " can not modify";
This defines nothing more than a single pointer that is initialized
to point to a literal string, i.e. a string that resides in memory
that you''re not allowed to modify.
p[0] =''b'' ;is not allowed, but if you declare p as
char p[] = "can modify";




这个定义完全不同。它为你提供了一系列字符,

(足够大,可以容纳字符串中的多个字符可以

修改,还有一个额外的尾随' '\'''字符)和

初始化为该字符串。所以第二个声明是

基本上是
的缩写

char p [sizeof" can modify" ];

strcpy(p,可以修改);


而第一个定义不涉及任何隐式strcpy()

但是只是在指针的不可修改的内存中分配一个地址。


问候,Jens


-

\ Jens Thoms Toerring ___ Je ********** *@physik.fu-berlin.de

\ __________________________ http://www.toerring.de


[新手答案]
ba ********* @ gmail.com 写道:
[newbie answer]
ba*********@gmail.com wrote:
定义时
char * p ="不能修改;

p [0] =''b'';不允许,


p包含字符串的地址不能修改。

这个字符串可以在任何地方 - 甚至在ROM中。


你可以改变''p''本身,而不是它的内容。

但如果你宣布p为
char p [] ="可以修改" ;;


''p''在这里创建,与int相同;

其内容随后/复制/从字符串can修改"

可以在任何地方 - 甚至在ROM上。

p [0] =''b'';好吗?
为什么?


您正在更改常量的副本,并且该副本是可更改的。

两个声明+定义之间有什么区别?
when define
char *p = " can not modify";

p[0] =''b'' ;is not allowed,
p contains the address of the string " can not modify".
This string can be anywhere -- even in ROM.

You can change ''p'' itself, not its contents.
but if you declare p as
char p[] = "can modify";
''p'' is created here, the same way as a int would;
Its contents are then /copied/ from the string "can modify" which
can be anywhere -- even on ROM.
p[0] = ''b''; is ok?
why?
You''re changing a copy of the constant, and that copy is changeable.
what''s difference between the two declaration + definition?



使用第一个''p''为指针分配空间。


使用第二个''p''你可以分配多少字符串中所有

字符(包括终止''\ 0'')所需的空格。


HTH

-

如果每个人都读到USENET将是一个更好的地方:|给我发邮件:简单地|
http:/ /www.catb.org/~esr/faqs/smart-questions.html | "答复"在这篇文章中,|
http://www.netmeister.org /news/learn2quote2.html | * NO * MIME,纯文本|
http://www.expita。 com / nomime.html |和* NO *附件。 |


With the first ''p'' you allocate space for a pointer.

With the second ''p'' you allocate as much space as needed for all the
characters (including the terminating ''\0'') in the string.

HTH
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |


使用:char * p =" something",你声明指向字符串的指针

" something",以及许多编译器将字符串放在const部分,所以

更改此数据并不好。有些编译器只是将字符串放入数据中

部分(我认为所有Borland编译器,但不是肯定的)并且改变了这个

数据是可以的。但是,这个来源是不可移植的。


使用:char p [] =" something",你声明数组,而不是指向字符串的指针。

因为数组是合理的,你可以更改数组的值。


PS

即使MinGW也没有发出任何关于此的警告,但代码不会工作。

With: char *p = "something", You declare pointer that points to string
"something", and many compilers will place string in const section, so
changing this data is not good. Some compilers just put string in data
section (I think all Borland compilers, but not for sure) and changin this
data is OK. But, this source will not be portable.

With: char p[] = "something", You declare array, not pointer to string.
Becuase p is array it is logical that You can change value of array.

P.S.
Even MinGW does not issue any warning about this, but code will not work.


这篇关于为什么char * p =“不能修饰”; char p [] =“可以修改”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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