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

发布于 2024-07-22 05:54:47 字数 102 浏览 9 评论 0原文

有什么区别:

char * const 

const char *

What's the difference between:

char * const 

and

const char *

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

坦然微笑 2024-07-29 05:54:47

区别在于 const char * 是指向 const char 的指针,而 char * const 是指向 char 的常量指针

首先,所指向的值不能更改,但指针可以。 第二,指向的值可以改变,但指针不能(类似于引用)。

还有一个

const char * const

which是一个指向常量char的常量指针(所以它的任何内容都不能改变)。

注意:

以下两种形式是等效的:

const char *

and

char const *

其确切原因在 C++ 标准中进行了描述,但请务必注意并避免混淆。 我知道几个编码标准更喜欢:

char const

over

const char

(有或没有指针),以便 const 元素的位置与指针 const 的位置相同。

The difference is that const char * is a pointer to a const char, while char * const is a constant pointer to a char.

The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).

There is also a

const char * const

which is a constant pointer to a constant char (so nothing about it can be changed).

Note:

The following two forms are equivalent:

const char *

and

char const *

The exact reason for this is described in the C++ standard, but it's important to note and avoid the confusion. I know several coding standards that prefer:

char const

over

const char

(with or without pointer) so that the placement of the const element is the same as with a pointer const.

梦里寻她 2024-07-29 05:54:47

为了避免混淆,请始终附加 const 限定符。

int       *      mutable_pointer_to_mutable_int;
int const *      mutable_pointer_to_constant_int;
int       *const constant_pointer_to_mutable_int;
int const *const constant_pointer_to_constant_int;

To avoid confusion, always append the const qualifier.

int       *      mutable_pointer_to_mutable_int;
int const *      mutable_pointer_to_constant_int;
int       *const constant_pointer_to_mutable_int;
int const *const constant_pointer_to_constant_int;
九八野马 2024-07-29 05:54:47

const 总是修改它前面的东西(它的左边),除非它是类型声明中的第一个东西,它修改它后面的东西(它的右边) )。

所以这两个是相同的:

int const *i1;
const int *i2;

它们定义指向 const int 的指针。 您可以更改 i1i2 指向的位置,但无法更改它们指向的值。

这:

int *const i3 = (int*) 0x12345678;

定义了一个指向整数的 const 指针,并将其初始化为指向内存位置 12345678。您可以更改地址 12345678 处的 int 值,但不能更改i3 指向的地址。

const always modifies the thing that comes before it (to the left of it), EXCEPT when it's the first thing in a type declaration, where it modifies the thing that comes after it (to the right of it).

So these two are the same:

int const *i1;
const int *i2;

they define pointers to a const int. You can change where i1 and i2 points, but you can't change the value they point at.

This:

int *const i3 = (int*) 0x12345678;

defines a const pointer to an integer and initializes it to point at memory location 12345678. You can change the int value at address 12345678, but you can't change the address that i3 points to.

假装不在乎 2024-07-29 05:54:47

经验法则:从右到左阅读定义!


const int *foo;

表示“foo 指向 (*) 一个无法更改的 int (常量)”。
对于程序员来说,这意味着“我不会更改 foo 指向的”。

  • *foo = 123;foo[0] = 123; 无效。
  • 允许 foo = &bar;

int *const foo;

表示“foo 无法更改 (const) 并指向 (*) 一个 int”。
对于程序员来说,这意味着“我不会更改 foo 引用的内存地址”。

  • 允许使用 *foo = 123;foo[0] = 123;
  • foo = &bar; 无效。

const int *const foo;

表示“foo 无法更改 (const) 并将 (*) 指向 < code>int 无法更改 (const)”。
对于程序员来说,这意味着“我不会更改 foo 指向的,也不会更改 foo 的地址 指的是“。

  • *foo = 123;foo[0] = 123; 无效。
  • foo = &bar; 无效。

Rule of thumb: read the definition from right to left!


const int *foo;

Means "foo points (*) to an int that cannot change (const)".
To the programmer this means "I will not change the value of what foo points to".

  • *foo = 123; or foo[0] = 123; would be invalid.
  • foo = &bar; is allowed.

int *const foo;

Means "foo cannot change (const) and points (*) to an int".
To the programmer this means "I will not change the memory address that foo refers to".

  • *foo = 123; or foo[0] = 123; is allowed.
  • foo = &bar; would be invalid.

const int *const foo;

Means "foo cannot change (const) and points (*) to an int that cannot change (const)".
To the programmer this means "I will not change the value of what foo points to, nor will I change the address that foo refers to".

  • *foo = 123; or foo[0] = 123; would be invalid.
  • foo = &bar; would be invalid.
暖阳 2024-07-29 05:54:47

const char* 是指向常量字符的指针
char* const 是指向字符的常量指针
const char* const 是指向常量字符的常量指针

const char* is a pointer to a constant character
char* const is a constant pointer to a character
const char* const is a constant pointer to a constant character

风吹短裙飘 2024-07-29 05:54:47

const * char 是无效的 C 代码,没有意义。 也许您想问 const char *char const * 之间的区别,或者可能是 const char * 和 a 之间的区别char * const

另请参阅:

const * char is invalid C code and is meaningless. Perhaps you meant to ask the difference between a const char * and a char const *, or possibly the difference between a const char * and a char * const?

See also:

快乐很简单 2024-07-29 05:54:47
  1. const char* x 这里 X 基本上是一个指向常量值的字符指针

  2. char* const x 是指字符指针,该指针是常量,但指向的位置可以改变。

  3. const char* const x是1和2的组合,表示它是一个常量字符指针,指向常量值。

  4. const *char x 将导致编译器错误。 它不能被声明。

  5. char const * x 等于点 1。

经验法则是如果 const 与 var name 一起使用,那么指针将是常量,但指向的位置可以更改,否则指针将指向常量位置,并且指针可以指向另一个位置,但是指向位置内容无法更改

  1. const char* x Here X is basically a character pointer which is pointing to a constant value

  2. char* const x is refer to character pointer which is constant, but the location it is pointing can be change.

  3. const char* const x is combination to 1 and 2, means it is a constant character pointer which is pointing to constant value.

  4. const *char x will cause a compiler error. it can not be declared.

  5. char const * x is equal to point 1.

the rule of thumb is if const is with var name then the pointer will be constant but the pointing location can be changed , else pointer will point to a constant location and pointer can point to another location but the pointing location content can not be change.

秋凉 2024-07-29 05:54:47

另一个经验法则是检查 const 位于何处:

  1. before * => 存储为常数
  2. *后 => 指针本身是常量

Another thumb rule is to check where const is:

  1. before * => value stored is constant
  2. after * => pointer itself is constant
呢古 2024-07-29 05:54:47

第一个是语法错误。 之间的区别

const char * mychar

也许您的意思是和

char * const mychar

在这种情况下,第一个是指向无法更改的数据的指针,第二个是始终指向同一地址的指针。

First one is a syntax error. Maybe you meant the difference between

const char * mychar

and

char * const mychar

In that case, the first one is a pointer to data that can't change, and the second one is a pointer that will always point to the same address.

末蓝 2024-07-29 05:54:47

许多答案提供了特定的技术、经验法则等来理解变量声明的特定实例。 但有一个通用的技术可以理解任何声明:

顺时针/螺旋规则

A)

const char *a;

根据顺时针/螺旋规则 a 是指向常量字符的指针。 这意味着字符是不变的,但指针可以改变。 即 a = "other string"; 没问题,但 a[2] = 'c'; 将无法编译

B)

char * const a;

根据规则, a< /code> 是指向字符的 const 指针。 即您可以执行 a[2] = 'c'; 但不能执行 a = "other string";

Lots of answer provide specific techniques, rule of thumbs etc to understand this particular instance of variable declaration. But there is a generic technique of understand any declaration:

Clockwise/Spiral Rule

A)

const char *a;

As per the clockwise/spiral rule a is pointer to character that is constant. Which means character is constant but the pointer can change. i.e. a = "other string"; is fine but a[2] = 'c'; will fail to compile

B)

char * const a;

As per the rule, a is const pointer to a character. i.e. You can do a[2] = 'c'; but you cannot do a = "other string";

屋顶上的小猫咪 2024-07-29 05:54:47

我想你的意思是 const char * 和 char * const 。

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

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

然后是 const char * const ,其中指针和字符不能改变。

I presume you mean const char * and char * const .

The first, const char *, is a pointer to a constant character. The pointer itself is mutable.

The second, char * const is a constant pointer to a character. The pointer cannot change, the character it points to can.

And then there is const char * const where the pointer and character cannot change.

北恋 2024-07-29 05:54:47

这里有详细的代码解释

/*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;
}*/

Here is a detailed explanation with code

/*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;
}*/
双马尾 2024-07-29 05:54:47

char * const 和 const char *?

  1. 指向常量值

const char * p; // 值不能更改

  1. 指向值的常量指针

char * const p ; // 地址不能更改

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

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

char * const and const char *?

  1. Pointing to a constant value

const char * p; // value cannot be changed

  1. Constant pointer to a value

char * const p; // address cannot be changed

  1. Constant pointer to a constant value

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

沩ん囻菔务 2024-07-29 05:54:47
// 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’
}
// 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’
}
自找没趣 2024-07-29 05:54:47
  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;
}
  1. 指向 const 值的指针:在这种情况下,指针可以指向任意数量的相应类型的变量,但我们无法在特定时间更改指针所指向的对象的值。

语法:

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. Constant pointer: A constant pointer can point only to a single variable of the respective data type during the entire program.we can change the value of the variable pointed by the pointer. Initialization should be done during the time of declaration itself.

Syntax:

datatype *const var;

char *const comes under this case.

/*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;
}
  1. Pointer to a const value: In this a pointer can point any number of variables of the respective type but we cannot change the value of the object pointed by the pointer at that specific time.

Syntax:

const datatype *varor datatype const *var

const char* comes under this case.

/* 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;
    }
等待我真够勒 2024-07-29 05:54:47

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

这些都是“指向常量 char 的常量指针”的等效方式:

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

The const modifier is applied to the term immediately to its left. The only exception to this is when there is nothing to its left, then it applies to what is immediately on its right.

These are all equivalent ways of saying "constant pointer to a constant char":

  • const char * const
  • const char const *
  • char const * const
  • char const const *
秉烛思 2024-07-29 05:54:47

两条规则

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

例如

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

Two rules

  1. If const is between char and *, it will affect the left one.
  2. If const is not between char and *, it will affect the nearest one.

e.g.

  1. char const *. This is a pointer points to a constant char.
  2. char * const. This is a constant pointer points to a char.
三生一梦 2024-07-29 05:54:47

我想指出的是,使用 int const * (或 const int *)并不是指向 const int 变量的指针,但是对于这个特定的指针来说,这个变量是 const 。

例如:

int var = 10;
int const * _p = &var;

上面的代码编译得很好。 _p 指向一个 const 变量,尽管 var 本身不是常量。

I would like to point out that using int const * (or const int *) isn't about a pointer pointing to a const int variable, but that this variable is const for this specific pointer.

For example:

int var = 10;
int const * _p = &var;

The code above compiles perfectly fine. _p points to a const variable, although var itself isn't constant.

最舍不得你 2024-07-29 05:54:47

我记得在关于 C 的捷克书上:阅读从变量开始并向左走的声明。
因此,

char * const a;

您可以读作:“a 是指向 char 的常量指针类型的变量”,

char const * a;

您可以读作:“a 是一个指针char 类型的常量变量我希望这会有所帮助:

const char * const a;

将读到 a 是指向 char 类型的常量变量。

I remember from Czech book about C: read the declaration that you start with the variable and go left.
So for

char * const a;

you can read as: "a is variable of type constant pointer to char",

char const * a;

you can read as: "a is a pointer to constant variable of type char. I hope this helps.

Bonus:

const char * const a;

You will read as a is constant pointer to constant variable of type char.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文