传递一系列bools / ints来改变它

时间:2014-03-13 21:51:29

标签: c++ arrays

我已经在一个函数中定义了一个bools数组(也有一个包含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’

非常感谢任何建议!

2 个答案:

答案 0 :(得分:1)

只需声明functionWithAltering,就像这样:

bool functionWithAltering (bool Byte[], int...) {
    ...
}

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

至于你对布尔数组的使用:如果你想存储的只是0或1,那么这是一个非常有效和明智的选择。

答案 1 :(得分:0)

对数组的引用的正确声明将采用以下方式

bool functionWithAltering( bool ( &Byte )[8], int...){
    ...
}

此外,不是将参数声明为引用数组,而是可以使用两个参数:指向数组的指针及其大小

bool functionWithAltering( bool Byte[], size_t size,  int...){
    ...
}
相关问题