0

I have the following code

int vertical[] = {0, 1};
int horizontal[] = {1, 0};
int diag1[] = {1, 1}, diag2[] = {1, -1};

int directions[][] = {vertical, horizontal, diag1, diag2};

Which gives me the following error on line 5:

Array has incomplete element type 'int []'

Thus, I'm stuck doing the following:

int directions[4][2] = {{0,1},{1,0},{1,1},{1,-1}};

How can I define directions[][] using the four 1d arrays?

1
  • How about C++ arrays: std::array? We are in 2021 with C++20 and people are still using C arrays. Commented Mar 21, 2021 at 0:34

1 Answer 1

1

You can use std::array

#include <array>
#include <iostream>

int main() {
    std::array vertical {0, 1};
    std::array horizontal {1, 0};
    std::array diag1 {1, 1}, diag2 {1, -1};

    std::array directions {vertical, horizontal, diag1, diag2};
    for (const auto dir : directions) {
        for (const auto val : dir) {
            std::cout << val << ' ';
        }
        std::cout << '\n';
    }
}
Sign up to request clarification or add additional context in comments.

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.