gpt4 book ai didi

c++ - 将 bool /整数数组传递给应该改变它的函数

转载 作者:行者123 更新时间:2023-11-28 07:01:38 25 4
gpt4 key购买 nike

我在一个函数中定义了一个 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...) {
...
}

函数参数中的数组总是衰减为指向第一个元素的指针 - 它们永远不会通过拷贝传递,因此您不必担心拷贝可能效率低下。这也意味着调用者始终可以看到 functionWithAltering() 中对 Byte[i] 的任何修改。

至于您对 bool 数组的使用:如果您只想存储 0 或 1,这是一个非常有效且明智的选择。

关于c++ - 将 bool /整数数组传递给应该改变它的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22391738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com