const int*、const int * const 和 int const * 有什么区别?

What is the difference between const int*, const int * const, and int const *?(const int*、const int * const 和 int const * 有什么区别?)
本文介绍了const int*、const int * const 和 int const * 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是搞砸如何正确使用 const int*const int * constint const *.是否有一套规则来定义你能做什么和不能做什么?

I always mess up how to use const int*, const int * const, and int const * correctly. Is there a set of rules defining what you can and cannot do?

我想知道在分配、传递给函数等方面的所有注意事项.

I want to know all the do's and all don'ts in terms of assignments, passing to the functions, etc.

推荐答案

向后阅读(由 顺时针/螺旋规则):

  • int* - 指向 int 的指针
  • int const * - 指向 const int 的指针
  • int * const - 指向 int 的 const 指针
  • int const * const - 指向 const int 的 const 指针
  • int* - pointer to int
  • int const * - pointer to const int
  • int * const - const pointer to int
  • int const * const - const pointer to const int

现在第一个 const 可以在类型的任一侧,所以:

Now the first const can be on either side of the type so:

  • const int * == int const *
  • const int * const == int const * const
  • const int * == int const *
  • const int * const == int const * const

如果你真的想发疯,你可以这样做:

If you want to go really crazy you can do things like this:

  • int ** - 指向 int 的指针
  • int ** const - 一个指向 int 指针的 const 指针
  • int * const * - 指向 const 的指针,指向 int 的指针
  • int const ** - 一个指向 const int 的指针
  • int * const * const - 一个 const 指针,指向一个 const 指针,指向一个 int
  • ...
  • int ** - pointer to pointer to int
  • int ** const - a const pointer to a pointer to an int
  • int * const * - a pointer to a const pointer to an int
  • int const ** - a pointer to a pointer to a const int
  • int * const * const - a const pointer to a const pointer to an int
  • ...

为了确保我们清楚 const 的含义:

And to make sure we are clear on the meaning of const:

int a = 5, b = 10, c = 15;

const int* foo;     // pointer to constant int.
foo = &a;           // assignment to where foo points to.

/* dummy statement*/
*foo = 6;           // the value of a can´t get changed through the pointer.

foo = &b;           // the pointer foo can be changed.



int *const bar = &c;  // constant pointer to int 
                      // note, you actually need to set the pointer 
                      // here because you can't change it later ;)

*bar = 16;            // the value of c can be changed through the pointer.    

/* dummy statement*/
bar = &a;             // not possible because bar is a constant pointer.           

foo 是一个指向常量整数的变量指针.这使您可以更改指向的内容,但不能更改指向的值.最常见的情况是 C 风格的字符串,其中有一个指向 const char 的指针.您可以更改指向的字符串,但不能更改这些字符串的内容.当字符串本身位于程序的数据段中并且不应更改时,这一点很重要.

foo is a variable pointer to a constant integer. This lets you change what you point to but not the value that you point to. Most often this is seen with C-style strings where you have a pointer to a const char. You may change which string you point to but you can't change the content of these strings. This is important when the string itself is in the data segment of a program and shouldn't be changed.

bar 是指向可以更改的值的常量或固定指针.这就像一个没有额外语法糖的参考.由于这个事实,通常你会使用一个引用来使用 T* const 指针,除非你需要允许 NULL 指针.

bar is a constant or fixed pointer to a value that can be changed. This is like a reference without the extra syntactic sugar. Because of this fact, usually you would use a reference where you would use a T* const pointer unless you need to allow NULL pointers.

这篇关于const int*、const int * const 和 int const * 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

C++ find method not working(C++查找方法不起作用)
How to determine the offset of an element of a tuple at compile time?(如何在编译时确定元组元素的偏移量?)
Error: control Reaches end of non void function(错误:控件已到达非无效函数的末尾)
Error: Jump to case label in switch statement(错误:跳转到SWITCH语句中的CASE标签)
Order of elements in set of pointers(指针集中元素的顺序)
error while loading shared libraries: jvm.dll(加载共享库时出错:jvm.dll)