C : const char\*const\* 的含义

新手上路,请多包涵

在其中一个 C++ 程序中,我看到了一个函数原型: int Classifier::command(int argc, const char*const* argv)

const char*const* argv 是什么意思?和 const char* argv[] 一样吗? const char** argv 是否也意味着相同?

原文由 vigs1990 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 898
2 个回答

不,它与 const char *argv[]const 禁止在特定的取消引用级别修改取消引用的值:

 **argv = x; // not allowed because of the first const
*argv = y; // not allowed because of the second const
argv = z; // allowed because no const appears right next to the argv identifier

原文由 Blagovest Buyukliev 发布,翻译遵循 CC BY-SA 3.0 许可协议

来自 C++ 超级常见问题解答

从右到左阅读指针声明。

  • const X* p means ” p points to an X that is const “: the X object can’t be 通过 p 更改。
  • X* const p means ” p is a const pointer to an X that is non-const “: you can’t更改指针 p 本身,但您可以 通过 p 更改 X 对象。
  • const X* const p means “p is a const pointer to an X that is const “: you can’t change the pointer p 本身,也不能通过 p 更改 X 对象。

而且,哦,是的,我是否提到要从右到左阅读您的指针声明?

const char * const *char const * const * 相同:一个(非 const)指针,指向一个 const 指针,指向一个 const 字符。

const char *char const * 相同:指向 const char 的(非 const)指针。

const char * *char const * * 相同:一个(非常量)指针,指向一个(非常量)指向常量字符的指针。

原文由 Samuel Harmer 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题