Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
char const数组可能的组合结果和效果_C_Arrays_Pointers_Char_Constants - Fatal编程技术网

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

char const数组可能的组合结果和效果,c,arrays,pointers,char,constants,C,Arrays,Pointers,Char,Constants,我正在尝试比较C中char数组(字符串)的不同声明 我主要比较了这两种组合的两点(而不是反复写下它们的名字): 点更改或点分配:我们只更改指针指向的内容。 char*a、*b a=b//我们正在这样做 值更改:我们正在更改指针指向的数据。 char*a *a='x'//我们正在这样做 下面是不同组合的代码。我对此有大约10个疑问。我必须一起问他们,因为他们都有某种联系 每一个疑问都在代码中解释。此外,还添加了错误消息 因为你可能不知道每个问题的答案。因此,我在代码中标记了不同的部分。并给出每个问

我正在尝试比较
C
char数组(字符串)
的不同声明

我主要比较了这两种组合的两点(而不是反复写下它们的名字):

  • 点更改或点分配:我们只更改指针指向的内容。
    char*a、*b
    a=b//我们正在这样做
  • 值更改:我们正在更改指针指向的数据。
    char*a
    *a='x'//我们正在这样做
  • 下面是不同组合的代码。我对此有大约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;
    }
    
    #包括
    int main(){
    char*p=“Something”//我无法更改数据
    char q[]=“Wierd”;//我可以更改q指向的内容
    //I___________________________
    printf(“\nI.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuchar*p.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n\n”);
    printf(“%s%s\n”,p,q);
    //*p='a';//由于无法更改值,导致出现分段错误
    p=q;//这是可能的,因为我改变了点
    //现在类型p是一个char指针,它不能改变值(因为我这样声明),但可以改变点
    //现在它指向一个char数组类型的内存,我可以改变它的值,但不能改变它的点
    //这意味着任务双方都有两个不同的东西,但gcc没有给出任何错误(即,它是可接受的)
    //即使用左侧变量类型的指针分配限制规则和值更改
    //使用了右侧变量类型的规则
    //(1)为什么?
    *p='x';
    printf(“%s%s\n”,p,q);
    //再做一次,试着做些更柔软的东西
    p=“某物”;
    q[0]=“W”;
    //二、字符q[]___________________________
    printf(“\nII.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuchar q[]\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n\n”);
    printf(“%s%s\n”,p,q);
    //q=p;//这是不可能的,因为对于q,我不能改变点。
    //这就是错误的来源
    //错误:从类型“char*”分配给类型“char[6]”时,类型不兼容
    *q='x';//这很好,因为可以更改q的值
    printf(“%s%s\n”,p,q);
    //再做一次,试着做些更柔软的东西
    p=“某物”;
    q[0]=“W”;
    //____________________________________________________________________/
    const char*r=“What”//我无法将数据更改为a所指向的内容(基本定义和const作用于同一对象)
    char const*s=“Point”//我无法将数据更改为a所指向的内容(基本定义和常量作用于同一对象)
    char*const t=“Pointers”;//我无法将数据更改为a指向的对象,因为基本定义和常量使c成为一个常量,而现在c只能指向单个实体。
    const char u[]=“Are”;//由于const,我无法将数据更改为a点的值,由于基本定义为[],我无法更改为d点的值。
    char const v[]=“Trying”;//由于const,我无法将数据更改为a点所指向的内容,并且由于[]的基本定义,我无法更改为d点所指向的内容。
    //char w const[]=“要生成”;//这是不可能的
    //___________________________________________________________________/
    //III.uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu___________________________
    printf(“\nIII.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n\n”);
    printf(“%s\n”,r);
    //*r='x';//这是不可能的
    //错误是:
    //错误:分配只读位置“*r”
    //现在,r的行为与p相同,但不是得到分段错误,而是在编译时得到了一个错误。
    //此外,此处设置的限制常量与p中存在的限制常量相同,只是(错误检查)。
    //结论:这意味着在这里写const在价值和意义上没有区别。以前是一样的,现在是一样的。
    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';
    //上述四宗个案
    //一切都适用于点分配
    //值更改无效(每次分配到只读位置错误,无分段错误)
    //一切都适用于点分配-这意味着一切都适用于
    //指针赋值左侧变量类型的规则(即使对于r=u,r=v,r=q)。
    //(2) 为什么会发生这种情况。(实际答案与原因(1)相关)
    //任何东西都不能改变价值观
    //现在这是荒谬的。乍看起来,好像事情发生在p=q的时候,这里
    //对于r=u,r=v,r=q,同样的事情也应该发生。但是仔细观察,你可以得到u,v
    //由于常量,对值更改有限制。
    //但是(3)为什么r=q的值没有变化?
    //(4)为什么fot r=p,r=q由于常量而导致错误,而不是由于分段错误。
    //重设细纹
    r=“什么”;
    //四、字符常量___________________________
    printf(“\nIV.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuun\n”);
    printf(“%s\n”,s);
    //*s='x';//这是不可能的
    //错误来了就是
    //错误:分配只读位置'*s'
    //s的行为与r完全相同
    //结论:在char之后或之前编写const没有区别。
    s=r;
    printf(“%s%s\n”,s,s);
    //*s='x';
    s=t;
    printf(“%s
    
    char *p = "Something";//I cant change the data
    char q[] = "Wierd"; // I can change to what q points to
    
    p = q;
    
    *p  = 'x';
    
    p = "Something";
    q[0] = 'W';
    
    //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
    
    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
    
    //*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;
    
    r = 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))
    
    //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.
    
    //Conclusion: Writing const after or before char makes no difference.
    
    //*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;
    
    //*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
    
    //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.)
    
    char q[] = "Wierd";