0

I have a problem. I need to create a code where I can access bool array information from another function, edit the array and then send it back. I need to use a variable as the size of the array.

Global variable is not an option.

I've tried to pass it by a reference and also using structs.

code for example:

void x(bool (&reserved[sizeOfArray)) {
    if (reserved[1] == true) {
          cout << "it's true";

}    

main() {

int sizeOfArray = 6;
bool reserved[sizeOfArray];

x(reserved[sizeOfArray];

edit: the size of the array is determined when the program is already running

6
  • 1
    This is not valid C++ code. Commented Dec 15, 2021 at 13:31
  • If the array size needs to be a variable then your best option is std::vector, variable length arrays are not supported in C++ Commented Dec 15, 2021 at 13:34
  • The name of the array is "reserved", not "reserved[sizeOfArray]". reserved[sizeOfArray] would be a bool, if it existed. Commented Dec 15, 2021 at 13:35
  • 1
    If you want to use the most readable C++ syntax : std::array<bool,6> reserved; void x(std::array<bool,6>& reserved){} Commented Dec 15, 2021 at 13:53
  • Have you tried accessing it via pointer. You can also pass array size as another function argument. Commented Dec 15, 2021 at 13:58

4 Answers 4

1

If the size is only determined at runtime, you essentially have two options:

  1. Use a dynamically-sized container, like std::vector

    void x(std::vector<bool>& reserved)

  2. Use the "C method" of passing a pointer to the array's first element along with the array size
    void x(bool reserved[], size_t size)

The possible third option of having a "sentinel" value last in the array (like C-strings) won't work with bool, since you only have two values to choose from.

Sign up to request clarification or add additional context in comments.

Comments

1

I think the lightweight method would be to use std::span instead.

#include <iostream>
#include <span>

void x(std::span<bool> reserved) {
    reserved[1] = false;
}    

int main() {

    constexpr size_t sizeOfArray = 6;
    bool reserved[sizeOfArray]{false, true};

    x(reserved);

    if (reserved[1] == false) {
        std::cout << "Hello!" << std::endl;
    }
}

Comments

0

You could make it a function template, with the array size as a template parameter:

#include <iostream>

template<size_t N>
void x(bool (&reserved)[N]) {

    // make sure that the array size is at least 2 to not get out of
    // bounds access in reserved[1]:

    if constexpr (N >= 2) {    
        if (reserved[1] == true) {
            std::cout << "it's true\n";
        }
    }
}    

int main() {
    constexpr size_t sizeOfArray = 6; // must be a constant expression
    
    bool reserved[sizeOfArray]{false,true}; // rest will be false

    x(reserved);
}

Output:

It's true

4 Comments

How about using std::span instead?
@vikram Sure, that'd work too but the above is pretty close to what OP had in his/her code already - for better or for worse.
I agree with that and I like your answer. But std::span makes the code less daunting and clearer. Perhaps just a personal opinion.
@vikram I like the span approach too. Oh, you added an answer for it. Here, have an upvote :)
0

You can use boost::dynamic_bitset for dynamic vectors of bools. [Demo]

#include <boost/dynamic_bitset.hpp>
#include <iostream>  // cout

void x(const boost::dynamic_bitset<>& reserved) {
    if (reserved[1] == true) {
          std::cout << "it's true\n\n";
    }
    for (int i{0}; i < reserved.size(); ++i)
    {
        std::cout << "Bit " << i << ": " << reserved[i] << "\n";
    }
}

int main() {
    int sizeOfArray = 6;
    boost::dynamic_bitset<> reserved(sizeOfArray, 0b111010);
    x(reserved);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.