c - what does this 2 const mean?

China☆狼群 提交于 2019-11-29 12:34:40

First const tells you can not change *key, key[i] etc

Following lines are invalid

*key = 'a';
*(key + 2) = 'b';
key[i] = 'c';

Second const tells that you can not change key

Following lines are invalid

key = newkey;
++key;

Also check how to read this complex declaration


Adding more details.

  1. const char *key: you can change key but can not change the chars pointed by key.
  2. char *const key: You can not change key but can the chars pointed by key
  3. const char *const key: You can not change the key as well as the pointer chars.

const [type]* means that it's a pointer that does not change the pointed value. [type]* const means that the value of the pointer itself cannot change, ie, it keeps pointing to the same value, akin to the Java final keyword.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!