This question already has answers here:
Initialize multidimensional array with zeros
(7 answers)
Expression does not evaluate to a constant
(1 answer)
Variable Length Array (VLA) in C++ compilers
(2 answers)
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 2 months ago.
I am trying to initialize a 2d array (matrix) to 0 but, cant do it:
int longestCommonSubsequence(string text1, string text2) {
int len1=0;
len1=text1.length()+1;
int len2=0;
len2=text2.length()+1;
int dp[len1][len2]={0};
Error:
Line 8: Char 16: error: variable-sized object may not be initialized
int dp[len1][len2]={0};
^~~~
1 error generated.
I want initialize the matrix while declaring it. I do not want to use for loop.
Variable sized arrays are not standard C++, for arrays of constant length you just use value-initialisation syntax:
int arr[4][4]{};
Alternatively, use C++ collections of variable length:
int rows = 4;
int columns = 4;
std::vector<std::vector<int>> vec(rows, std::vector<int>(columns));
Related
This question already has answers here:
C++ : Creating an array with a size entered by the user
(3 answers)
How to create a dynamic array of integers
(8 answers)
Closed 1 year ago.
I know variable length arrays are not allowed in c++. My code is currently as follows:
int main(){
int t;
cin >> t;
double h[t];
}
How can I create an array with length t then?
Edit: the assignment only allows char, bool, int and double. Vector isn't allowed. When I tried to compile it, it says ISO C++ forbids variable length array 'h'.
This question already has answers here:
how to create a contiguous 2d array in c++?
(7 answers)
Closed 3 years ago.
OK, this question seems to be silly but bear with me. When I trying to create a 2D array in C++, it gave me some warnings (len is an integer):
double a[len][len];
// warning: variable length arrays are a C99 feature
// warning: variable length array used
So I tried another:
double **a = new double[len][len];
// error: only the first dimension of an allocated array may have dynamic size
// read of non-const variable 'len' is not allowed in a constant expression
How can I do it correctly in C++11?
double** a=new double*[len];
for(int i=0;i<len;++i)
{
a[i]=new double[len];
}
Are there any restrictions on what you can use? If you're planning to do array manipulations I'd say just use [Eigen] (http://eigen.tuxfamily.org/index.php?title=Main_Page)
This question already has answers here:
How to create an array when the size is a variable not a constant?
(6 answers)
Closed 3 years ago.
How to use the the value stored in a int variable as the size of my array
eg.
int a = 40;
int b[a]; // a = 40
You can't; not in standard C++ anyway, unless a is a constexpr or const integral type with a positive value.
The best alternative is a std::vector<int> b(a);
Create an array with new[]:
int *b = new int[a];
but remember to delete it later with:
delete[] b;
A better alternative is the std container, like std::vector.
This question already has answers here:
Array size at run time without dynamic allocation is allowed? [duplicate]
(8 answers)
Closed 4 years ago.
Total c++ newbie here.
I have a problem, where I can not assign the number of character in a string to an array size, like so..😓
string outStr;
ifstream input("read.txt");
getline(input, outStr);
int const n = outStr.length();
int arr[n];
error msg --> expression must have a constant value. although i have declared the "const"
Thanks in advance✌.
C++ does not support Variable Length Arrays. Use a std::vector instead.
This question already has answers here:
In C++ books, array bound must be constant expression, but why the following code works?
(2 answers)
Closed 5 years ago.
As C++ primer said, we can't use variable as the dimension of builtin array, so the following code did not work
int length = 3;
int array[length] = {0, 1, 2};
the error is
error: variable-sized object may not be initialized
But why following code works?
int length = 3;
int array[length];
This is an extension by your compiler called a Variable Length Array (VLA) and is not in the C++ standard which means that this code can break at any moment you switch compilers or the compiler vendor decides to no longer support this feature. If you want a variable length array that does not depend on this extension but instead on the standard you should use a std::vector.