2

I want to declare length of array with global variable. But I got error, I am confused for solving this.

int BARIS = 8;
//I get error when declare Squares Array with global variable (BARIS variable)
int SquaresInfo[BARIS];

int main(int argc, const char * argv[]) {
    cout << "\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t Tekan ENTER untuk masuk ke permainan";
    if(cin.get() == '\n' ) {
        system("clear");
        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
        gameInterface();
    } else {
        cout << " ";
    }

    return 0;
}

enter image description here

I get error when declare length of SquareInfo array. I am confusing how to declare array length with global variable

4
  • The size of an array must be a compile-time constant. Apr 4, 2015 at 16:56
  • 1
    Add const to BARIS definition.
    – kvorobiev
    Apr 4, 2015 at 16:56
  • @kvorobiev if you answer not comment I will choose you as best answer Apr 4, 2015 at 17:34
  • You still could vote it up)
    – kvorobiev
    Apr 4, 2015 at 17:41

4 Answers 4

2

The size of an array shall be a constant expression (greater than 0). Change this declaration

int BARIS = 8;

to

const int BARIS = 8;

The C++ Standard does not allow Variable Length Arrays (VLA) though some compilers have their own language extensions that allow to use VLA(s). And moreover VLA(s) shall have automatic storage duration. That is if you are allowed to define a VLA then it shall be a local array

From the C Standard where VLA(s) are allowed (6.7.6.2 Array declarators)

  1. ...If an identifier is declared to be an object with static or thread storage duration, it shall not have a variable length array type.

If the variable may not be constant then use std::vector<int> instead of the array. For example

#include <vector>

//...

int BARIS = 8;
std::vector<int> SquaresInfo( BARIS );
0
2

Besides the other answers may point out solutions for the c-style array definition (you need a constexpr that can be used to specify the array size at compile time), I'd like to encourage using std::array<> as proposed by the current standard instead:

std::array<int,8> SquaresInfo;

The current size can be obtained using the SquaresInfo::size() function elsewhere.

Accessing certain indices will be bounds checked, and also it provides much more type like behavior (e.g. automatic copying along assignment operations), than a c-style array does.

0

Just move the array definition inside your main function and/or declare BARIS const

0

You can't have a variable array index, try

#define BARIS 8
int squareInfo[BARIS];
3
  • or better, std::array<int, BARIS>.
    – Robinson
    Apr 4, 2015 at 17:00
  • 1
    I certainly would prefer const int BARIS=8. Apr 4, 2015 at 17:04
  • but gcc test.cpp will throw error for const int as index; (g++ command will complete without any errors)
    – Vishnu V
    Apr 4, 2015 at 17:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.