为什么在这种情况下递增阵列名称有效?
Why Incrementing Array name works in this case?
我了解到数组名称被视为常量指针,因此不能递增。但是,我发现它在这种情况下工作:当数组作为函数的参数传递时,在函数内部,数组的名称可以递增。有人可以解释为什么这有效吗?
这是代码:
using namespace std;
unsigned int c_in_str(const char* str, char ch);
int main()
{
char mmm[15] = "minimum";
unsigned int ms = c_in_str(mmm, 'm');
cout << "m counts: " <<ms;
return 0;
}
// this function counts the number of ch characters
unsigned int c_in_str(const char* str, char ch)
{
unsigned int count = 0;
while (*str) // quit when *str is ' '
{
if (*str == ch)
count++;
str++; // how come this works with array name?
}
return count;
}
确实不能递增数组,但str
指针而不是数组。
你可以通过查看 减速const char* str
.奇怪的是,即使您将声明更改为此const char str[]
,str
仍然是一个指针。在 C++(或 C(中不可能将数组作为参数。
相关文章:
- 欧拉项目#8答案是大以获得有效答案
- OpenMP阵列性能较差
- 调整大小后指向元素值的指针unordered_map有效?
- 为什么是0;C++中的有效语句
- 最高有效数字侧的第N位
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- GCC对可能有效的代码抛出init list生存期警告
- 当我的阵列太大时出现分段错误
- 有效地使用std::unordered_map来插入或增加键的值
- c++中O(n^(1/3))中一个数的除数的有效计数
- 为什么在这种情况下递增阵列名称有效?
- EIGEN地图类:将C阵列映射到VectorXD指针以有效的方式
- 用 1M 个条目有效地屏蔽了 30% 的阵列
- 查找有效内部圆形内部阵列的索引
- 如何在C/C 或CUDA中有效地通过对角有效地翻转char阵列
- 使用Emscripten C Web Worker的大型阵列有效传输:JavaScript设计更好
- 在尺寸n的3D阵列中有效检查相邻的体素
- C++删除不带括号的阵列内存仍然有效
- 增加阵列C++的最有效方法
- 顺时针和逆时针旋转阵列的有效方式