将一个bool /int数组传递给函数,该函数应该对其进行修改
Passing an array of bools/ints to function which should alter it
我在一个函数中定义了一个bool数组(也有int数组的版本-不确定当我只想存储1和0时哪个更好)。如何将它传递给另一个函数这个函数返回的是数组之外的东西?我正在参考,但我得到错误。
bool functionWithAltering (bool &(Byte[]), int...){
...
}
bool functionWhereSetting (.....) {
bool Byte[8];
....
if (!functionWithAltering(Byte, ...))
return 0;
bool Byte[16];
....
if (!functionWithAltering(Byte, ...))
return 0;
...
}
我得到的错误是:
error: declaration of ‘byte’ as array of references
error: expected ‘)’ before ‘,’ token
error: expected unqualified-id before ‘int’
非常感谢您的建议!
像这样声明functionWithAltering
:
bool functionWithAltering (bool Byte[], int...) {
...
}
函数实参中的数组总是衰变成指向第一个元素的指针——它们从不通过copy传递,因此您不必担心可能低效的复制。这也意味着在functionWithAltering()
中对Byte[i]
的任何修改都会被调用者看到。
对于布尔值数组的使用:如果你只想存储0或1,这是一个完全有效和明智的选择。
数组引用的正确声明如下
bool functionWithAltering( bool ( &Byte )[8], int...){
...
}
也可以使用两个形参:指向数组的指针和数组的大小
bool functionWithAltering( bool Byte[], size_t size, int...){
...
}
相关文章:
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何从子成员函数修改父公共成员变量
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- C++ 使用同一函数时如何修改间距
- C++:通过函数修改数组
- 如何调试指针参数是否通过函数修改
- 为什么我可以在C 11中使用const函数修改类
- Qt - 使用λ函数修改先前连接信号的插槽参数
- 如何返回通过memcpy函数修改的两个阵列
- Arduino esp8266中的c++函数修改了c文件中生成的浮点值,在c++函数退出后,c文件中会出现胡言乱语
- 调用带有扭曲的库函数-修改其某些行为
- 二维数组被函数修改后如何删除
- 由算术运算符成员函数修改的动态数组的复制构造函数
- 如何在函数修改堆栈时将堆栈传递给函数
- 当我们可以使用普通函数修改静态数据成员时,静态成员函数的需求是什么?
- 通过函数修改数组的元素
- 挂钩(热补丁)类成员函数.修改虚函数表项
- 为什么引用成员可以被const成员函数修改
- 使用 friend 关键字和使用成员函数修改类内部的私有变量有什么区别
- c++通过一个函数修改多个数组