• Register
1 vote
787 views

Problem:

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?

15.3k points

Please log in or register to answer this question.

2 Answers

2 votes

Solution:

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.

Thanks

94.3k 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.

Declaration:

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.

Solution:

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.

Code:

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.

3.9k points

Related questions

0 votes
1 answer 183 views
183 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 13k points
0 votes
1 answer 1.7k views
1.7k views
How c++ expression can have integral or unscoped type?
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 18 views
18 views
Problem: C++ expression must have constant value
asked Jul 20 asha 117k points
0 votes
1 answer 19 views
19 views
Problem: Array expression must have a constant value c++
asked Jul 4 nila 117k points
0 votes
1 answer 6 views
6 views
Problem: Expression must have a constant value
asked 3 days ago Chi Omega 166k points
0 votes
1 answer 13 views
13 views
Problem: Please,I want an answer : Function call must have a constant value in a constant expression
asked Jun 19 Chi Omega 166k points
0 votes
1 answer 53 views
0 votes
1 answer 6 views
6 views
Problem : Error expression must have a constant value.
asked Jul 11 Isac Christiaan 102k points
0 votes
1 answer 5 views
5 views
Problem : Hi, Is there a way to solve it: Typeerror: 'float' object cannot be interpreted as an index?
asked Jun 23 Sifat55 104k points
0 votes
1 answer 207 views
207 views
Problem: I'm trying to find an answer to this complication >Typeerror: 'tuple' object cannot be interpreted as an integer.
asked May 7 Danyal Sjoerd 19.2k points