0

I want to make array for positions (x, y, z)

It would be something like this:

/*Whatever the type*/ array[100] = {(1, 2, 3), (4, 2, 5)....}

How can I do this?

4
  • You cannot declare a variable name with void. Commented Feb 17, 2021 at 7:25
  • I update the question Commented Feb 17, 2021 at 7:25
  • 1
    int array[100][3] would have been fine. Commented Feb 17, 2021 at 7:31
  • Array with multiple data types – you can't store multiple data types in a static array, only a single type. Coordinates are usually represented with integers. Commented Feb 17, 2021 at 7:37

1 Answer 1

2

To be honest, making a struct makes the program much simplified:

struct location {
    int x;
    int y;
    int z;
};

int main(void) {
    location array[100];

    // Example initialization of the first index
    array[0].x = 10;
    array[0].y = 20;
    array[0].z = 30;

    // Or, even better...

    array[0] = {10, 20, 30};
    .
    .
}

In case you want to manually initialize x, y, z of each, then:

location array[100] = {{1, 4, 3}, {3, 0, 2}, {}, ...}

Note: These values are not required, they are just to demonstrate the initialization.

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

2 Comments

typedef struct ... is useless in C++. struct {...] location; is enough...
Why not follow their required initialization syntax and suggest location array[100] = {{1, 2, 3}, {4, 2, 5} /*...*/ };

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.