Peter Parker
Peter Parker

Reputation: 93

Creating an array with a variable

I have the following code:

#include <iostream>

using namespace std;

int main()
{
   unsigned int endx = 5;
   unsigned int endy = 5;
   unsigned int endz = 5;

   int Matrix[endx+1][endy+1][endz+1] = {};

   return 0;
}

I get

error C2057: expected constant expression

Ok, how can I create the Matrix like shown in the code without vectors or dynamic allocated array?

Upvotes: 1

Views: 53

Answers (2)

outlyer
outlyer

Reputation: 3943

As the error says, you need constant expressions to define array/matrix sizes; in that example, make integers const and it will compile.

const unsigned int endx = 5;
const unsigned int endy = 5;
const unsigned int endz = 5;

If you don't want dynamically allocated arrays or vectors, then you'll have to know the exact array size beforehand.

Upvotes: 2

Jay Miller
Jay Miller

Reputation: 2234

Arrays require constants when creating them. In this code, just making your end* variables const unsigned int should do it.

Upvotes: 2

Related Questions