• Register
1 vote


Hello all,
How to declare an array properly in c/c++? I am a new student of c++ and I am trying to declare two-dimensional arrays in my program. Unfortunately, my arrays are not working. Here is my sample code

int row = 5;
int col= 5;
int [row][col];

The above codes throw an error expression must have a constant value c++. What’s wrong with it?

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

2 votes


You’ve declared the variables row and col as a normal integer type. C++ doesn't allow non-constant values for the size of an array. That's just the way it was designed. In your scenario, you are trying to allocate an array length with unknown value. That’s the reason your compiler can't read the code and gives the error back. For array, you need to specify the length very clearly that the compiler can create the space and allocate the required memory. The compiler cannot make any assumptions about the contents of non-constant variables. So, you may re-write the code again this way

const int row = 5;
const int col= 5;
int myArray[row][col];

This const fixes the size of the array. Now I am sure you understand the problem well. Keep asking.


13 9 6
94,240 points
0 votes

You are trying to declare a two-dimensional array. It is the simplest form of a multidimensional array.

A two-dimensional array is, in essence, is a list of a one-dimensional array.


To declare a two-dimensional integer array of size x and y you have to write as;

Type arrayname [x][y];

Where type can be any valid C++ data type and arrayName will be a valid C++ identifier.

Two-dimensional array:

A two-dimensional array can be taken as a table, where x represents the number of rows and y represent the columns. So every element in array a is identified by an element name of the firm a[i][j], where a is the name of the array and I and j are the subscripts that uniquely identify each element in a.


So the error in your question is that you have not to use the identifier in the initialization of an array. You have declared the variables row and columns as a normal integer type. C++ does not allow nonconstant values for the size of an array. That is the reason, your compiler can not read the code and give the error back.


The correct code for your example is as follows;

const int row = 5;

const int col = 5;

int array[row][col];

Now I hope you can understand what is the error in your code and how to solve the error in your code.

11 5 2
3,890 points

Related questions

0 votes
1 answer 71 views
Problem: I have't coded in c++ for some time and I got stuck when I tried to compile this simple snippet: class A { public: void f() {} }; int main() { { A a; a.f(); // works fine } { A *a = new A(); a.f(); // this doesn't } }
asked Dec 26, 2020 Mashhoodch 9.1k points
0 votes
1 answer 813 views
How c++ expression can have integral or unscoped type?
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 6 views
0 votes
1 answer 8 views
Problem: I have this following code: int M = 3; int C = 5; int match = 3; for ( int k =0; k < C; k ++ ) { match --; if ( match == 0 && k = M ) { std::cout << " equals" << std::endl; } } But it gives out an error saying: Error: expression must be a ... trying to modify "match" or "k" value here, but why this error? if I only write it like: if ( match == 0 ) it is ok. Could someone explain it to me?
asked Dec 23, 2020 Mashhoodch 9.1k points
1 vote
1 answer 60 views
Problem: I have a sample c++ code below that throws me back an error: &ldquo;c++ expression must be a modifiable value&rdquo; What could be the reason? int A = 3; int B = 5; int match = 3; for ( int k =0; k < B; k ++ ) { match --; if ( match == 0 && k = A ) { std::cout << " equals" << std::endl; } } Thanks for your help.
asked Mar 10, 2020 Gavin 15.3k points
1 vote
2 answers 1.8K views
Problem: I've got a problem with c++ code. I am attaching the sample code snippet below. Please pull me up. Yee, of course, I am pretty new in C++ #include <iostream> using namespace std; class Myclass { public: void myfunc() { cout<<"Hello World"<<endl; } }; int main ... = new Myclass(); // myfunc is part of the class object, rather than // part of the pointer to that class' object. a.myfunc(); }
asked Mar 7, 2020 Gavin 15.3k points
0 votes
1 answer 3.6K views
Problem : I am very new to C++ and trying to learning C++ template. Please find below my code, I am trying to define a template class and also want to call the member function in the main() method.. template <typename T> class Calculate { T _x; T ... the last line of above code : Expression preceding parentheses of apparent call must have (pointer-to-) function type How can I resolve this issue?
asked Dec 3, 2019 alecxe 7.5k points
0 votes
1 answer 101 views
Problem: Hello programmers! I am learning to program, and I just got a little confused about boolean values and logical operators. I tried to run the following program: #include<iostream> using namespace std; int main() { bool check = false && true || true; ... program, I got the following output: Condition evaluated to true I am so confused about how this condition evaluates to true. Any help?
asked Dec 18, 2020 Code Learner 5.8k points
0 votes
1 answer 25 views
25 views asked Jan 28 khanboy 2.1k points
0 votes
1 answer 50 views
Problem : Write a program to evaluate an Infix expression using stack in C++. The program should have infix expression directly in the code.
asked Oct 8, 2020 sikandar 2.4k points