关于字符串文字的问题 [英] A question on string literals

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

问题描述

char * str1 =" Hello";

char arr1 [] = {" Hello" };

char arr2 [] = {''H'',''e'',''l'',''l'',''o''};


修改str1,arr1和arr2是否合法?

char *str1 = "Hello";
char arr1[] = { "Hello" };
char arr2[] = { ''H'', ''e'', ''l'', ''l'', ''o'' };

Is it legal to modify str1, arr1 and arr2 ?

推荐答案

1.char * str1 =" Hello";


通常,字符串文字将存储在生成的目标代码的只读数据中,后编译。因此,修改

由str1处理的内存位置的内容。会导致

seg错误。这可能与编译器有关。


虽然,str1本身可以修改。


以下是有效的:

char * str1 =" Hello" ;;

str1 = 0 ; //允许


2.char arr1 [] = {" Hello" };


由arr1寻址的内存的内容可以修改。 arr1

本身无法修改。


以下是无效的:

char arr1 [] = { "你好" };

arr1 = 0; //不允许。


3. char arr2 [] = {''H'',''e'',''l'',''l'',' 'o''};


与arr2相同的评论如上所述在这里坚持。


-Sriram。

1.char *str1 = "Hello";

Generally the string literals would be stored in the read only data
section of the resulting object code, post compilation. So, modifying
the contents of the memory location addressed by "str1" would result in
seg fault. This might be compiler dependent though.

Although, "str1" itself can be modified.

The following is valid:
char *str1 = "Hello";
str1 = 0; // Allowed

2.char arr1[] = { "Hello" };

Contents of the memory addressed by "arr1" can be modified. "arr1"
itself cannot be modified.

That is the following is invalid:
char arr1[] = { "Hello" };
arr1 = 0; // Not allowed.

3. char arr2[] = { ''H'', ''e'', ''l'', ''l'', ''o'' };

Same comments as above for "arr2" hold true here.

-Sriram.


junky_fel ... @ yahoo.co.in写道:
junky_fel...@yahoo.co.in wrote:
char * str1 =" Hello" ;;
char arr1 [] = {" Hello" };
char arr2 [] = {''H'',''e'','l'',''l'',''o''};
修改str1,arr1和arr2是否合法?
char *str1 = "Hello";
char arr1[] = { "Hello" };
char arr2[] = { ''H'', ''e'', ''l'', ''l'', ''o'' };

Is it legal to modify str1, arr1 and arr2 ?




这是一个FAQ。阅读问题1.32


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

Krishanu



This is a FAQ. Read Question 1.32

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

Krishanu


ju**********@yahoo.co。在写道:
char * str1 =" Hello" ;;
char arr1 [] = {" Hello" };
char arr2 [] = {''H'',''e'','l'',''l'',''o''};
修改str1,arr1和arr2是否合法?
char *str1 = "Hello";
char arr1[] = { "Hello" };
char arr2[] = { ''H'', ''e'', ''l'', ''l'', ''o'' };

Is it legal to modify str1, arr1 and arr2 ?




FAQ 6.2应该是一个很好的起点。


引用草稿:


声明

char arr1 [] = {" Hello"
定义一个''普通''char数组对象(未知大小)`arr1'',其中

元素

初始化为字符串文字。数组内容

是可修改的。此声明与

char arr2 [] = {''H'','e'','l'','l'',''o''}相同;

以及

char arr3 [] = {''H'',''e'',''l'',''l'',' 'o'',''\ 0''};


OTOH,

char * str1 =" Hello";

定义str1,类型为''指向char'的指针'并初始化它

指向一个类型为''char'数组的对象,长度为

6其元素用字符串文字初始化。

如果尝试使用p修改数组的内容,

行为未定义。



FAQ 6.2 should be a good place to start.

To quote the draft:

The declaration
char arr1[] = { "Hello" };
defines a ''plain'' char array object (of unknown size) `arr1'' whose
elements
are initialized with character string literal. Array contents
are modifiable. This declaration is identical to
char arr2[] = { ''H'', ''e'', ''l'', ''l'', ''o'' };
as well as
char arr3[] = { ''H'', ''e'', ''l'', ''l'', ''o'', ''\0'' };

OTOH,
char *str1 = "Hello";
defines str1 with type ''pointer to char'' and initializes it
to point to an object with type ''array of char'' with length
6 whose elements are initialized with a character string literal.
If an attempt is made to use p to modify the contents of the array,
the behavior is undefined.


这篇关于关于字符串文字的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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