char const array可能的组合结果和效果

Abh*_*pta 1 c arrays pointers const char

我想比较不同的声明char array (string)C.

我有两点主要比较这些组合(而不是一次又一次地写我命名):

  1. 点更改或点分配:我们只更改指针指向的内容.
    char *a, *b;
    a=b //we are doing this
  2. 值更改:我们正在更改指针指向的数据.
    char *a;
    *a='x' //we are doing this

以下是具有不同组合的代码.我有大约10个疑点.我必须一起问他们,因为他们都有某种联系.

每个疑问都在代码中解释.此外,还添加了错误消息.

因为你可能不知道每个问题的答案.所以,我在代码中标记了不同的部分.并且还给出了每个问题的编号.
如果您知道任何问题的答案请用适当的索引回答.

在代码中,我也提出了自己的观察和结论,这可能是错误的.所以我用结论:标记标记了它们.如果您发现任何结论错误,请分享/回答.

码:

#include <stdio.h>

int main() {

    char *p = "Something";//I cant change the data
char q[] = "Wierd"; // I can change to what q points to

// I. ______________________  char*p   ___________________________
printf("\nI. ______________________  char*p   ___________________________ \n\n");

printf("%s %s\n",p, q);
//*p = 'a';// got segmentation fault as I cant change the Value


p = q;//This is possible because I change the Point

//Now the type p is a char pointer which can't change Value (because I declared it like this) but can change the Point
//and it is now pointing to a memory which is of type a char array.I can change its Value but cant change its Point
//This means there are two different things on both sides of the assignment but gcc doesnot give any error (i.e. it is acceptable)
//That is for Pointer assignment restriction rules of the type of left side var was used and for Value change
//rules of the type of right side var was used
// (1)Why?

*p  = 'x';
printf("%s %s\n",p, q);

//Again try to make Something Wierd
p = "Something";
q[0] = 'W';


// II. ______________________  char q[]   ___________________________
printf("\nII. ______________________  char q[]   ___________________________ \n\n");

printf("%s %s\n",p, q);
//q = p;// This is not possible because for q I cant change Point.
// This is the error comes
//error: incompatible types when assigning to type ‘char[6]’ from type ‘char *’

*q = 'x';//This works fine as this is possible to change Value for q
printf("%s %s\n",p, q);

//Again try to make Something Wierd
p = "Something";
q[0] = 'W';

//____________________________________________________________________/

const char * r = "What";//I cant change the data to what a points to (basic def and const act on same)
char const * s = "Point";//I cant change the data to what a points to (basic def and const act on same)
char * const t = "Pointers";//I cant change the data to what a points to because of basic def and const make c a const that now c can only point to single entity.

const char u[] = "Are";//I cant change the data to what a points to because of const and I can't change to what d points because of basic def of [].
char const v[] = "Trying";//I cant change the data to what a points to because of const and I can't change to what d points because of basic def of [].
//char w const [] = "To make";//This is not possible

//___________________________________________________________________/


// III. ______________________  const char * r   ___________________________
printf("\nIII. ______________________  const char * r   ___________________________ \n\n");

printf("%s\n",r);

//*r = 'x'; // This is not possible
//Error comes is:
//error: assignment of read-only location ‘*r’
//now the behaviour of r is same as p but instead of getting segmentation fault I got an error at compile time.
//Also the restriction const put here is same as of restriction present with p except(error checking).
//Conclusion : This means writing const here makes no difference in terms of Value and Point. What it was before is the same now.

r = s;
printf("%s %s\n",r ,s);
//*r = 'x';

r = t;
printf("%s %s\n",r ,t);
//*r = 'x';

r = u;
printf("%s %s\n",r ,u);
//*r = 'x';

r=v;
printf("%s %s\n",r ,v);
//*r = 'x';

r=p;
printf("%s %s\n",r ,p);
//*r = 'x';

r=q;
printf("%s %s\n",r ,q);
//*r = 'x';

//For above four cases
//Everything works for Point assignment 
//Nothing Works for Value change (Everytime assignment to read-only location error, no segmentation fault)

//Everything Works for Point assignment - This means everything works for the 
//rules of the type of varible on the left side for Pointer assignment. (Even for r=u,r=v, r=q).
//(2) WHY this is happening.(Actualy answer related to WHY(1))

//Nothing Works for Value change
//Now this is absurd. On the first look it seems that as the things happen at the time of p=q, here
//for r=u, r=v, r=q same things should had happened. But on the closer inspection you can get that u,v 
//have restrictions on Value change because of const.
//But (3)Why no Value change is happening for r=q ? 
// (4) Why fot r=p, r=q getting error due to const. not due to segmentation fault.



//Resetting Wierdness
r = "What";

// IV. ______________________  char const * s   ___________________________
printf("\nIV. ______________________  char const * s   ___________________________ \n\n");

printf("%s\n",s);

//*s = 'x'; // This is not possible
//Error comes is 
//error: assignment of read-only location ‘*s’
//Behavious of s is exactly same as r
//Conclusion: Writing const after or before char makes no difference.


s = r;
printf("%s %s\n",s ,s);
//*s = 'x';

s = t;
printf("%s %s\n",s ,t);
//*s = 'x';

s = u;
printf("%s %s\n",s ,u);
//*s = 'x';

s=v;
printf("%s %s\n",s ,v);
//*s = 'x';

s=p;
printf("%s %s\n",s ,p);
//*s = 'x';

s=q;
printf("%s %s\n",s ,q);
//*s = 'x';

//For above four cases
//Everything happens same as with r.


//Resetting Wierdness
s = "Point";

// V. ______________________  char * const t   ___________________________
printf("\nV. ______________________  char * const t   ___________________________ \n\n");

printf("%s\n",t);

//*t = 'x';//This is not possible
//Error is
//Segmentation-fault
//This means that on Value change the error comes not due to const. It comes for the same reason of p.

//t = r;
printf("%s %s\n",t ,r);
//*t = 'x';

//t = s;
printf("%s %s\n",t ,s);
//*t = 'x';

//t = u;
printf("%s %s\n",t ,u);
//*t = 'x';

//t=v;
printf("%s %s\n",t ,v);
//*t = 'x';

//t=p;
printf("%s %s\n",t ,p);
//*t = 'x';

//t=q;
printf("%s %s\n",t ,q);
//*t = 'x';

//For above four cases
//Nothing Works for Point Assignment
//Nothing works for value change (Everytime segmentation fault, assignment to read-only location error)

//Nothing Works for Point Assignment
//This is understandable

//Nothing works for value change
// (5) Why this is happening. Why left hand side is always given precedence. Why this isn't happening p=q, 
//because for value change t=q and p=q are exctly same both pn left side and right side of the assignment.


//Resetting Wierdness
//t = "Pointers"; //No need


// VI. ______________________  const char u[]   ___________________________ 
printf("\nVI. ______________________  const char u[]   ___________________________   \n\n");

printf("%s\n",u);

//*u = 'x';//This is not possible
//Error Comes is
//error: assignment of read-only location ‘*(const char *)&u’
//This error comes because of const.
//Conclusion: [] gives the Point restriction and const gives the Value Restriction  


//u = r;
printf("%s %s\n",u ,r);
//*u = 'x';

//u = s;
printf("%s %s\n",u ,s);
//*u = 'x';

//u = t;
printf("%s %s\n",u ,t);
//*u = 'x';

//u=v;
printf("%s %s\n",u ,v);
//*u = 'x';

//u=p;
printf("%s %s\n",u ,p);
//*u = 'x';

//u=q;
printf("%s %s\n",u ,q);
//*u = 'x';

//For above four cases
//Nothing Works for Point Assignment
//Nothing works for value change (Everytime assignment to read-only location error, no segmentation fault)

//Nothing Works for Point Assignment
//Error Comes for each is:  
//warning: assignment of read-only location ‘u’ [enabled by default]
//error: incompatible types when assigning to type ‘const char[4]’ from type ‘const char *’

//Left side rules are given precedence. (6)Why? (If already not solved in above answers)

//Nothing works for value change    
//Left side rules are given precedence. (7)Why? (If already not solved in above answers)




//Resetting Wierdness
//u = "Are";

// VII. ______________________  char const v[]   ___________________________    

printf("\nVII. ______________________  char const v[]   ___________________________  \n\n");

printf("%s\n",v);

//*v = 'x';//This is not possible
//Error Comes is
//error: assignment of read-only location ‘*(const char *)&v’
//This error comes because of const.
//Conclusion: Writing const after or before char makes no difference.



//v = r;
printf("%s %s\n",v ,r);
//*v = 'x';

//v = s;
printf("%s %s\n",v ,s);
//*v = 'x';

//v = t;
printf("%s %s\n",v ,t);
//*v = 'x';

//v=u;
printf("%s %s\n",v ,u);
//*v = 'x';

//v=p;
printf("%s %s\n",v ,p);
//*v = 'x';

//v=q;
printf("%s %s\n",v ,q);
//*v = 'x';

//For above four cases
//Everything works as same with u.


//Resetting Wierdness
//v = "Trying";


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// (8) WHY `const char * a;`  and  `char const * a`  works same?????


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//---------------Now doing more Possible combinations with p and q------------------
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// VIII. ~~~~~~~~~~~~~~~~~~~~~~~~~~~ char *p ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("\nVIII. ~~~~~~~~~~~~~~~~~~~~~~~~~~~ char *p ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n\n");

//p = r;
printf("%s %s\n",p ,r);
//*p = 'x';

//p = s;
printf("%s %s\n",p ,s);
//*p = 'x';

//p = t;
printf("%s %s\n",p ,t);
//*p = 'x';

//p=u;
printf("%s %s\n",p ,u);
//*p = 'x';

//p=v;
printf("%s %s\n",p ,v);
//*p = 'x';

//For above four cases

//Point Assignment
//Warning for p=r, p=s is
//warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
//Conclusion:Kind of understandable.

// NO Warning for p=t 
//For left side type I can do Point assignment and for Right Side I can,t do.
// Left side rules are given precedence. (9)Why? (If already not solved in above answers)

//Warning for p=u, p=v is
//warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
//For left side type I can do Point assignment and for Right Side I can,t do.
// Left side rules are given precedence. (10)Why? (If already not solved in above answers)


//Value Change
//Segmentation fault for everything .
//Conclusion: Understndable if assume left hand side are given precedence except for p = q (showed in I.)


//Resetting Wierdness
p = "Something";

// IX. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ char q[] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("\nIX. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ char q[] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n\n");

//q = r;
printf("%s %s\n",q ,r);
*q = 'x';

//q = s;
printf("%s %s\n",q ,s);
*q = 'x';

//q = t;
printf("%s %s\n",q ,t);
*q = 'x';

//q=u;
printf("%s %s\n",q ,u);
*q = 'x';

//q=v;
printf("%s %s\n",q ,v);
*q = 'x';

//For above four cases

//Point Assignment
//Error for each is:
//error: incompatible types when assigning to type ‘char[6]’ from type ‘const char *’
//Conclusion: Understandable, if assume L.H.S. is given precedence except for p = q (showed in I.)


//Value Change
//Possible for each
//Conclusion: Understndable if assume left hand side are given precedence except for p = q (showed in I.)


//Resetting Wierdness
*q = 'W'; //No need


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

丹尼尔的答案已经完成.但更简单的两个疑问:
1)对于pcase对象是一个字符串文字(哪一个不能修改),即它是右手边对象的属性,而对于RHS的q我有相同的东西,但现在它表现不同.为什么这么不一致的设计.
2)对于p如果我修改字符串文字,行为是未定义的,即有时它被修改,有时不修改.为什么这样的设计.实际上,对于out of bound数组访问,这是可以理解的,因为你访问之前没有分配的内存,有时你没有权限访问那段内存所以,分段错误.但有时为什么我可以修改字符串文字.为什么这样.这背后的原因是什么?

Dan*_*her 5

char *p = "Something";//I cant change the data
char q[] = "Wierd"; // I can change to what q points to
Run Code Online (Sandbox Code Playgroud)

pchar*指向char不允许修改的十个数组的第一个元素(不要忘记0终结符)(尝试修改字符串文字是未定义的行为;大多数实现将字符串文字存储为只读内存,然后这样的尝试会导致段错误,但尝试修改字符串文字可能会修改数组而不会崩溃).您可以p自由更改,当它更改为指向可修改对象时,您可以通过修改该对象*p.

q是一个六char的数组(再次,0终止).您不能分配任何值q,但您可以修改数组的内容.

p = q;
Run Code Online (Sandbox Code Playgroud)

你让我们p指向数组的第一个元素q.在该上下文中,q隐式转换为指向其第一个元素的指针,实际发生的情况也是&q[0]如此p = &q[0];.现在p指向一个可修改的对象

*p  = 'x';
Run Code Online (Sandbox Code Playgroud)

是允许的,改变所述第一charq.

p = "Something";
q[0] = 'W';
Run Code Online (Sandbox Code Playgroud)

p再次指向char不允许修改的数组的第一个元素,并将第一个元素更改q回修改之前的内容p,这次使用q.

//q = p;// This is not possible because for q I cant change Point.
// This is the error comes
//error: incompatible types when assigning to type ‘char[6]’ from type ‘char *’
Run Code Online (Sandbox Code Playgroud)

错误消息有点误导,您无法分配数组.即使使用char hello[] = "Hello"; char world[] = "World";,虽然两个数组都具有相同的类型,但您无法分配,hello = world; produces the same error (since in that context,world`将转换为指向其第一个元素的指针,但错误消息并没有错误.

*q = 'x';//This works fine as this is possible to change Value for q
Run Code Online (Sandbox Code Playgroud)

对,*q就是q[0]这样,所以你可以在很多情况下使用像指针这样的数组(反之亦然).但总的来说,数组和指针是不同类型的东西.

const char * r = "What";//I cant change the data to what a points to (basic def and const act on same)
char const * s = "Point";//I cant change the data to what a points to (basic def and const act on same)
Run Code Online (Sandbox Code Playgroud)

const char *并且char const *表示完全相同,指向不可修改的指针char(通常是这类数组中的第一个).

char * const t = "Pointers";//I cant change the data to what a points to because of basic def and const make c a const that now c can only point to single entity.
Run Code Online (Sandbox Code Playgroud)

t是一个常量指针,你不能改变这里它指向的,但每个类型,你可以修改它指向的对象.但是,在这种情况下,t指向char字符串文字的第一个,因此不允许将对象t点更改为(但这不是t's类型的结果).

const char u[] = "Are";//I cant change the data to what a points to because of const and I can't change to what d points because of basic def of [].
char const v[] = "Trying";//I cant change the data to what a points to because of const and I can't change to what d points because of basic def of [].
Run Code Online (Sandbox Code Playgroud)

同样,两者都是相同的,u并且v是数组const char,由于它们的类型,不允许更改这些数组的内容.

//char w const [] = "To make";//This is not possible
Run Code Online (Sandbox Code Playgroud)

是的,这是无效的语法,你不能在数组名和括号之间有一个类型限定符.

//*r = 'x'; // This is not possible
//Error comes is:
//error: assignment of read-only location ‘*r’
Run Code Online (Sandbox Code Playgroud)

r禁止改变指向对象的类型r(但通过其他指针改变它是合法的).

//now the behaviour of r is same as p but instead of getting segmentation fault I got an error at compile time.
//Also the restriction const put here is same as of restriction present with p except(error checking).
//Conclusion : This means writing const here makes no difference in terms of Value and Point. What it was before is the same now.
Run Code Online (Sandbox Code Playgroud)

结论是错误的,如果你r改为指向一个可修改的对象,q例如,你仍然无法改变它r,那么类型禁止它.但你可以修改它p.对于字符串文字的指针,通常不同的是*r = 'x';编译失败和*p = 'x';分段错误,但是对于一般情况,修改p是有效的(如果p指向a的元素,通常甚至"有效" const char arr[10],但这又是未定义的行为,它通常不会导致崩溃,与字符串文字的对比).

r = s;
Run Code Online (Sandbox Code Playgroud)

好的没问题.您更改了哪个对象r指向,即您更改r,但您不更改r指向的对象.

r = t;
Run Code Online (Sandbox Code Playgroud)

也没问题.如果t指向可修改的对象,则可以通过t但不通过修改对象r.但是作为t字符串文字的指向,你t不能通过任何一个修改对象指向,但同样,一个是编译错误,另一个可能是段错误.

//Everything Works for Point assignment - This means everything works for the 
//rules of the type of varible on the left side for Pointer assignment. (Even for r=u,r=v, r=q).
//(2) WHY this is happening.(Actualy answer related to WHY(1))
Run Code Online (Sandbox Code Playgroud)

const为"只读".如果const char *r;有一个指针指向char编译器将不允许您使用它来修改指向对象的指针,则只能使用它来读取对象.它指向的对象是否被声明为const无关紧要,constin r的声明仅限制r可用于的内容,而不是通过其他指针对指向对象可以做什么.

//Nothing Works for Value change
//Now this is absurd. On the first look it seems that as the things happen at the time of p=q, here
//for r=u, r=v, r=q same things should had happened. But on the closer inspection you can get that u,v 
//have restrictions on Value change because of const.
//But (3)Why no Value change is happening for r=q ? 
// (4) Why fot r=p, r=q getting error due to const. not due to segmentation fault.
Run Code Online (Sandbox Code Playgroud)

我不确定我是否理解你的"为什么(3)",但如果我理解正确,你希望*r = 'x';能够继续工作r = q;,从此r指向一个可修改的对象.然后答案是我上面写的,声明中的const限定符r限制了你可以做的事情r,它独立const于指向对象的状态.这也回答了(4),r禁止转让的类型*r = whatever;.

//Conclusion: Writing const after or before char makes no difference.
Run Code Online (Sandbox Code Playgroud)

const*,尽管如此,它是出现在之前还是之后都有所不同.const char *r;声明一个不能用来修改指针对象的指针,并char * const x = q;声明一个总是指向同一位置的指针; 您可以使用它来修改指向对象(如果允许的话).并const char * const y = q;声明一个无法更改的只读指针.

//*t = 'x';//This is not possible
//Error is
//Segmentation-fault
//This means that on Value change the error comes not due to const. It comes for the same reason of p.
Run Code Online (Sandbox Code Playgroud)

你做不到,*t = 'x';因为t碰巧指向一个字符串文字.如果你初步t指出q,那将是允许的.

//t = r;
Run Code Online (Sandbox Code Playgroud)

这是不允许的,因为你声明t是不可修改的,你不能改变指向的地方.

//(5)为什么会这样.为什么左手边始终优先.为什么没有发生这种情况p = q,//因为对于值的改变,t = q和p = q在赋值的左侧和右侧都是非常相同的.

我不确定问题是什么.你宣称tconst,这意味着你不能分配给t.它会是相同的const int i = 100;,你不会被允许i = 120;在你的程序中写.

//*u = 'x';//This is not possible
//Error Comes is
//error: assignment of read-only location ‘*(const char *)&u’
//This error comes because of const.
//Conclusion: [] gives the Point restriction and const gives the Value Restriction
Run Code Online (Sandbox Code Playgroud)

对.

//For above four cases

//Point Assignment
//Warning for p=r, p=s is
//warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
//Conclusion:Kind of understandable.
Run Code Online (Sandbox Code Playgroud)

不仅"有点".你正在分配const char*一个char*.指向的对象可能是不可修改的,但尝试通过它修改它char *p是正式有效的,如果指向的对象是可修改的,使用p它来修改它甚至是合法的.但是,如果指向的对象不可修改,则使用const限定符声明字符串文字,尝试修改它p是未定义的行为.因此丢弃const限定符是一件危险的事情,编译器会对此发出警告.然而,它可能是完全合法的,所以它只是一个警告,而不是一个错误.您可以通过使用显式强制转换告诉编译器您知道自己在做什么(无论您是否这样做).

// NO Warning for p=t 
//For left side type I can do Point assignment and for Right Side I can,t do.
// Left side rules are given precedence. (9)Why? (If already not solved in above answers)
Run Code Online (Sandbox Code Playgroud)

你分配了char * const一个char*,这里什么都没有丢失.分配值t不会改变它,const之后*只能说你不能改变t指向的地址.

//Warning for p=u, p=v is
//warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
//For left side type I can do Point assignment and for Right Side I can,t do.
// Left side rules are given precedence. (10)Why? (If already not solved in above answers)
Run Code Online (Sandbox Code Playgroud)

与...相同p = r;.

//Value Change
//Segmentation fault for everything .
//Conclusion: Understndable if assume left hand side are given precedence except for p = q (showed in I.)
Run Code Online (Sandbox Code Playgroud)

分段错误只是因为你让指针指向字符串文字.如果你让他们指出char[],分配*ptr = 'x';将工作或不编译,这取决于指针是否被声明指向const char或仅指向char.如果你输入几个数组const char,那么赋值当然也不会编译const char*,但它会编译char*(带有关于丢弃const限定符的警告),并且运行程序会调用未定义的行为(它可能不会崩溃并修改数组内容,但任何事情都可能发生).


关于其他问题:

1)对于p case对象是一个字符串文字(一个不能修改),即它是右手边对象的属性,而对于RHS的q,我有相同的东西,但现在它表现不同.为什么这么不一致的设计.

我不确定你的想法有什么不同.在两者中,char *p = "Something";char *p = q;,指向对象的属性确定p有效的用途.两者中的任何一个都不能通过其类型进行修改(两者都char[N]适用于某些N),但字符串文字是不可修改的,作为标准定义的特殊情况.

声明char *p;(这里没有初始化,但是否存在一个并不重要)声明p为可用于修改指向的指针.但是,这种修改指向对象的尝试是否有效,取决于指向对象的属性.

char q[] = "Wierd";
Run Code Online (Sandbox Code Playgroud)

将字符串文字的内容复制到数组q(包括0终止符),因此q使用字符串文字的可修改副本进行初始化.让p指向一些char阵列中q,使p点到一个修改的对象,并且这样的修改是有效的.

更确切地说,p指向一个const合格的对象const char c = 'C'; p = &c;是危险的,因为类型p不会阻止*p = 'x';编译 - 毕竟,编译器一般不知道在那一点是p指向一个const合格的对象,还是一个可修改的对象(赋值可能来自const char*指向可修改对象的赋值,或根本没有有效位置.

因此,赋值p = &c;会导致编译器发出警告(至少在编译器的警告级别足够高,但在gcc和clang中,例如,默认情况下启用),甚至中止编译时出错(gcc和clang)如果你通过-pedantic-errors旗帜就这样做).语言标准禁止该转让没有明确铸造const char *&c是一个char*,但只需要诊断,所以编译器就可以使它成为一个警告或错误.

如果你只使用指针从指向对象读取而它指向一个不可修改的对象,这是一个合法的用途,所以赋值不是无条件禁止的(对于强制转换,它是标准和甚至没有引起警告,因为演员告诉编译器"我知道我在做什么".

因此,是否*p = 'x';有效,只能由指向对象的属性确定.如果指向的对象是不可修改的(或者p根本没有指向有效的对象),则行为是未定义的.未定义的行为如何表现为未定义,但在这种情况下,通常要么是分段错误(如果对象驻留在写保护的内存区域中),要么修改指向对象,就像它被允许一样(如果只有类型使对象不可修改,并且实现不会采取额外措施来识别此类无效写入).对于字符串文字,由于历史原因而输入类型char[N],通常它们存储在程序的.rodata(只读数据)部分中,并且操作系统检测到写入该字符串的尝试并导致一个段错误.

2)对于p如果我修改字符串文字,行为是未定义的,即有时它被修改,有时不修改.为什么这样的设计.实际上,对于out of bound数组访问,这是可以理解的,因为你访问之前没有分配的内存,有时你没有权限访问那段内存所以,分段错误.但有时为什么我可以修改字符串文字.为什么这样.这背后的原因是什么?

实用主义.

定义语言的委员会不喜欢将实施者的手绑在一起.程序员有义务避免未定义的行为,如果他不这样做,无论发生什么事情.

从历史上看,据我所知,有些实现将字符串文字存储在只读存储器中,而实存则没有.当语言被标准化时(创建后差不多二十年,所以行为有很多不同),它主要是对现有常见做法的记录.由于编译器,库或硬件的差异,行为差异很大,因此未定义或实现定义,以允许尽可能多的平台上的符合实现.因此,一些实现允许修改字符串文字,而其他实现则没有,委员会决定通过使行为未定义来给程序员带来负担.