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)
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:
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:
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:
Flexible Array Member (Zero Length Array) [duplicate]
(2 answers)
How to include a dynamic array INSIDE a struct in C?
(8 answers)
Closed 8 years ago.
I have seen the usage of this statement in driver programs at the end of the structure.
Can anyone explain me what is the use of this statement? and how does it works internally?
I mean will compiler considers it as array or a variable?
In C, it's a trick to allow you to put a variable-sized array at the end of a structure, by allocating enough memory for both the fixed-sized fields and whatever you want in the array. For example:
struct array {
size_t size;
int a[]; // strictly, it should be incomplete rather than zero sized
};
struct array * make_array(size_t size) {
struct array * array = malloc(sizeof (struct array) + size * sizeof (int));
array->size = size;
return array;
}
struct array * array = make_array(2);
array->a[1] = 42; // No problem: there's enough memory for two array elements
In C++, it's not valid. Use std::vector instead.
While arrays of size 0 are not supported by either standard, many compilers allow them as an extension. The standardised C way (C99+) instead leaves out the size altogether.
Thiis is used to describe a data-structure consisting of the starting fields and a variable number of array elements, as well as for comfortable access to them.
This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 10 years ago.
I have an integer num that was read from a file. I want to create an array with the number of elements being num.
A sample code of what I want to do but doesn't work:
int num;
cin >> num;
int iarray[num];
Arrays in C++ have compile-time bounds.
Use dynamic allocation instead, or a healthy std::vector wrapper around the same process.
dynamic allocation being int * iarray = new int[num];
Just make sure to call delete[] iarray; at some point to free the memory.