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'.
Related
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));
This question already has answers here:
Are variable length arrays there in c++?
(2 answers)
Array[n] vs Array[10] - Initializing array with variable vs numeric literal
(1 answer)
Closed 1 year ago.
Given that user input is taken at runtime but array is created during compile time. How and why does this piece of code work?
#include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n]; //Why can I use this statement to create an array?
}
It's a non-standard extension, supported by gcc and clang.
It works by allocating space on the stack at the point the array is declared, a bit like alloca.
The memory allocated is automatically freed (by adjusting the stack pointer) when the function returns (or arr goes out of scope).
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:
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:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 8 years ago.
Why I can't use this code when I want to assign the size of array entered by user to array?
int n;
cin>>n;
int array[n];
And is there another way of doing this instead of using this construction?
int n;
cin>>n;
int *array;
array = new int[n];
According to O'Reilly "C++ In A Nutshell" (2003),
An array is specified with a constant size in square brackets
Since your variable n is not a constant, it can't be used to specify the size of the array.
The same paragraph also says,
For an array-like container whose size can change at runtime, see <vector> in Chapter 13.
Sorry, but you are not allowed to have this construction.