康斯特指针C++斯特劳斯特鲁普
Const pointer C++ Stroustrup
int count_x(const char* p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to nothing)
{
if (p==nullptr)
return 0;
int count = 0;
for (; *p!=0; ++p)
if (*p==x)
++count;
return count;
}
p 是一个指针。常量表示指针无法修改。但是在 for 循环中,有 ++p,这意味着指针正在迭代/递增以访问值 *p
那里有一个矛盾 - p 不能修改,但它正在增加/修改?
C++中的声明从右到左读取。所以像
const char* p
将改为:p
是指向const char
的非常量指针。
显然,p
不是const
,但它所指向的是const
。所以*p = 's'
是非法的,但p++
不是。
相关文章:
- 康斯特指针C++斯特劳斯特鲁普
- C++:根据斯特劳斯特鲁普的例子播种随机数
- C++:斯特劳斯特鲁普iterator_traits示例不编译?
- packaged_task示例问题?斯特劳斯特鲁普示例修改
- C++ 斯特劳斯特鲁普的"std_lib_facilities.h"字符串结构 – 警告:无符号表达式的比较
- 为什么当作为常量字符* 返回时会在此处创建临时字符串?[斯特劳斯特鲁普的书例]
- 如何使用埃拉托斯特内斯筛算法改进素数程序
- 斯特劳斯特鲁普 第四章 练习 11.
- 斯特劳斯特鲁普 第 4 章 练习 6:提高效率
- 斯特劳斯特鲁普在函数中传递参数的准则
- 第4章 斯特劳斯特鲁普钻头一个具有挑战性的一步(至少对我来说!
- 斯特劳斯特鲁普使用C++的购买力平价:第7章,第7节("Recovering from errors")
- 康斯特指针浅拷贝
- 斯特劳斯特鲁普的"Simple_window"在按下按钮时收缩"Next"
- 我相信斯特劳斯图普的书,第三版第368页有一个错别字。有人可以确认吗?
- 为什么奥斯特林斯tream没有清空
- 模板 :: 斯特劳斯特鲁普的例子没有编译
- 在斯特劳斯特鲁普的PPP2e第7章钻头中找不到第三个错误
- 斯特劳斯特鲁普的P:PP 第4章 钻头:卡住
- 斯特劳斯特鲁普的C++书练习