char * const和const char *有什么区别?

366

两者之间有何不同:

char * const 

而且

const char *

4
可能是const int*, const int * const, 和 int const * 有什么不同?的重复问题。请注意,这个链接指向英语的stackoverflow网站。 - emlai
15
在“const”左侧的第一件事情是常量。如果“const”是最左边的东西,那么它右边的第一件事就是常量。 - Cupcake
9
友情提示,永远不要忘记cdecl这个工具。 - Braden Best
1
还有另一个char const*,它是exception::what()的返回类型。 - Zhang
19个回答

2

char * const和const char *有什么区别?

  1. 指向常量的指针

const char * p; // 值不能被改变

  1. 指向值的常量指针

char * const p; // 地址不能被改变

  1. 指向常量值的常量指针

const char * const p; // 两者都不能被改变。


2

这里有一个带有代码的详细解释:

/*const char * p;
char * const p; 
const char * const p;*/ // these are the three conditions,

// const char *p;const char * const p; pointer value cannot be changed

// char * const p; pointer address cannot be changed

// const char * const p; both cannot be changed.

#include<stdio.h>

/*int main()
{
    const char * p; // value cannot be changed
    char z;
    //*p = 'c'; // this will not work
    p = &z;
    printf(" %c\n",*p);
    return 0;
}*/

/*int main()
{
    char * const p; // address cannot be changed
    char z;
    *p = 'c'; 
    //p = &z;   // this will not work
    printf(" %c\n",*p);
    return 0;
}*/



/*int main()
{
    const char * const p; // both address and value cannot be changed
    char z;
    *p = 'c'; // this will not work
    p = &z; // this will not work
    printf(" %c\n",*p);
    return 0;
}*/

1
  1. 常量指针:常量指针在整个程序中只能指向相应数据类型的单个变量,我们可以改变指针所指变量的值。初始化应该在声明时完成。

语法:

datatype *const var;

char *const属于这种情况。

/*program to illustrate the behaviour of constant pointer */

#include<stdio.h>
int main(){
  int a=10;
  int *const ptr=&a;
  *ptr=100;/* we can change the value of object but we cannot point it to another variable.suppose another variable int b=20; and ptr=&b; gives you error*/
  printf("%d",*ptr);
  return 0;
}
  • 指向常量的指针:在这种情况下,指针可以指向相应类型的任意数量的变量,但我们不能在特定时间更改指针所指对象的值。
  • 语法:

    const datatype *vardatatype const *var

    const char*属于此类情况。

    /* program to illustrate the behavior of pointer to a constant*/
    
       #include<stdio.h>
       int main(){
           int a=10,b=20;
           int const *ptr=&a;
           printf("%d\n",*ptr);
           /*  *ptr=100 is not possible i.e we cannot change the value of the object pointed by the pointer*/
           ptr=&b;
           printf("%d",*ptr);
           /*we can point it to another object*/
           return 0;
        }
    

    1
    // Some more complex constant variable/pointer declaration.
    // Observing cases when we get error and warning would help
    // understanding it better.
    
    int main(void)
    {
      char ca1[10]= "aaaa"; // char array 1
      char ca2[10]= "bbbb"; // char array 2
    
      char *pca1= ca1;
      char *pca2= ca2;
    
      char const *ccs= pca1;
      char * const csc= pca2;
      ccs[1]='m';  // Bad - error: assignment of read-only location ‘*(ccs + 1u)’
      ccs= csc;    // Good
    
      csc[1]='n';  // Good
      csc= ccs;    // Bad - error: assignment of read-only variable ‘csc’
    
      char const **ccss= &ccs;     // Good
      char const **ccss1= &csc;    // Bad - warning: initialization from incompatible pointer type
    
      char * const *cscs= &csc;    // Good
      char * const *cscs1= &ccs;   // Bad - warning: initialization from incompatible pointer type
    
      char ** const cssc=   &pca1; // Good
      char ** const cssc1=  &ccs;  // Bad - warning: initialization from incompatible pointer type
      char ** const cssc2=  &csc;  // Bad - warning: initialization discards ‘const’
                                   //                qualifier from pointer target type
    
      *ccss[1]= 'x'; // Bad - error: assignment of read-only location ‘**(ccss + 8u)’
      *ccss= ccs;    // Good
      *ccss= csc;    // Good
      ccss= ccss1;   // Good
      ccss= cscs;    // Bad - warning: assignment from incompatible pointer type
    
      *cscs[1]= 'y'; // Good
      *cscs= ccs;    // Bad - error: assignment of read-only location ‘*cscs’
      *cscs= csc;    // Bad - error: assignment of read-only location ‘*cscs’
      cscs= cscs1;   // Good
      cscs= cssc;    // Good
    
      *cssc[1]= 'z'; // Good
      *cssc= ccs;    // Bad - warning: assignment discards ‘const’
                     //                qualifier from pointer target type
      *cssc= csc;    // Good
      *cssc= pca2;   // Good
      cssc= ccss;    // Bad - error: assignment of read-only variable ‘cssc’
      cssc= cscs;    // Bad - error: assignment of read-only variable ‘cssc’
      cssc= cssc1;   // Bad - error: assignment of read-only variable ‘cssc’
    }
    

    1

    我记得在一本关于C语言的捷克书中曾经读到过这样的声明:你需要从变量名开始向左阅读。所以对于

    char * const a;
    

    你可以这样理解:“a 是类型为常量指向 char 的指针变量”。
    char const * a;
    

    你可以这样阅读:“a是指向类型为char的常量变量的指针。希望这有所帮助。”
    奖励部分:
    const char * const a;
    

    你将会阅读到 a 是指向常量字符型变量的常量指针。

    1

    两个规则

    1. 如果const在char和*之间,则会影响左边的那个。
    2. 如果const不在char和*之间,则会影响最近的那个。

    例如:

    1. char const *。这是指向常量字符的指针。
    2. char * const。这是指向字符的常量指针。

    1

    const修饰符应用于其左侧的术语。唯一的例外是当它左侧没有任何内容时,它就应用于其右侧的内容。

    以下都是等效的表达方式,“常量指向常量char”:

    • const char * const
    • const char const *
    • char const * const
    • char const const *

    这是否与编译器有关?gcc对于“const char const *”、“const const char *”和“char const const *”产生相同的结果->指针可能指向其他位置。 - cosinus0

    1

    我想你是指 const char * 和 char * const。

    第一个 const char * 是指向常量字符的指针。指针本身是可变的。

    第二个 char * const 是指向字符的常量指针。指针不能改变,它所指向的字符可以改变。

    还有 const char * const,其中指针和字符都不能改变。


    你的前两个实际上是相同的,而第三个是编译器错误 :) - workmad3

    1
    我想指出使用int const *(或const int *)并不是指一个指针指向一个const int变量,而是这个变量对于这个特定的指针是const
    例如:
    int var = 10;
    int const * _p = &var;
    

    上面的代码可以完美编译。 _p 指向一个 const 变量,尽管 var 本身不是常量。

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接