A variable length array is an array in C99 and other languages whose size is unknown at compile time; instead, it's determined at runtime.
Questions tagged [variable-length-array]
413 questions
1
vote
0 answers
Variable-Length Arrays in c programming
I want to create an variable-length array.
Here is my code:
#include
int main (void)
{
int n,i, response;
while(true){
printf("Enter the number of responses:");
scanf("%i",&n);
if(n<1){
…

Daniel Yefimov
- 860
- 1
- 10
- 24
1
vote
2 answers
printf changes my output
I'm trying to solve this challenge on Hackerrank.
I've reached a problem where I cannot proceed, but I can't see where I've gone wrong - and I'm hoping someone here can help.
My current solution is as follows:
int main() {
int…
user6782645
1
vote
3 answers
code order with variable length array
In C99, is there a big different between these two?:
int main() {
int n , m;
scanf("%d %d", &n, &m);
int X[n][m];
X[n-1][m-1] = 5;
printf("%d", X[n-1][m-1]);
}
and:
int main(int argc, char *argv[]) {
int n , m;
int…

Austin
- 6,921
- 12
- 73
- 138
1
vote
3 answers
How to define array size based upon number of inputs?
As far as my knowledge, (im a beginner in c) you can define the size of an array provided the user knows how many inputs the user is going to provide. But, how can i define the size of an array based upon number of inputs?
For example, if I have to…

overmass123
- 21
- 1
- 4
1
vote
3 answers
The unknown size of array
In Java, I am trying to read files and then I want to put them in an array. But when I declare an array, an error occurs because of unknown length. Here's an example:
Object unsortedInt[];
try {
BufferedReader bR = new…

Sahin
- 63
- 1
- 1
- 4
1
vote
2 answers
C++1z(?) runtime sized array as a return value
Can we not do anything like this?
auto f(int n, char *arr[]) -> decltype(char *[n]) /* doesn't work */ {
char *tmp[n]; // RSA, the size depends on argc
for (int i = 0; i < n; ++i) tmp[i] = arr[i];
return tmp;
}
int main(int argc, char…

nodakai
- 7,773
- 3
- 30
- 60
1
vote
1 answer
Initialized Variable Length Array Working Variably (Between Computers)
I have some code that initializes arrays, specifically C-strings, using variables. For example...
int len = getLength();
char cstr[len+1] = {'\0'};
This compiles and runs flawlessly in my code. However, when one of my co-workers runs the same code,…

CodeMouse92
- 6,840
- 14
- 73
- 130
1
vote
1 answer
C99 Variable Length Array Max sizes and sizeof Function
I am experimenting with the use of Variable Length Arrays (VLAs) in my C code and trying to iron out my understanding of what they should and shouldn't do.
I have the following snippet from my function:
void get_pdw_frame_usb(pdws_t *pdw_frame,…

bph
- 10,728
- 15
- 60
- 135
1
vote
2 answers
Is there a good reason why VLA are not permitted in pointers in structs?
Here is a way to define a Matrix type
typedef struct {
int nr, nc;
double *elem;
} Matrix;
I would like to define this
typedef struct {
int nr, nc;
double elem[nr][nc];
} Matrix;
It would be nice, because I would not have to worry…
user1220978
1
vote
0 answers
Parsing function assigns proper value to array within itself but after call value is wrong
Having a problem with a 2D variable array of structs.
Trying to parse a text file and populate the array with the following function:
void populateTable(int x; int y; Entry table[x][y],
int x, int y, char fileName[])
{
…

J2R5M3
- 445
- 3
- 9
1
vote
2 answers
sizeof evaluation of a variable-length array
sizeof operand will evaluate the the operand if it is a variable-length array.
6.5.3.4, p2: If the type of the operand is a variable length array type, the operand is evaluated;
Yet this code is working, and I'm assuming it is defined:
#include…

this
- 5,229
- 1
- 22
- 51
1
vote
3 answers
Initialize array with a non-const function argument
Is there any way to initialize an array with a non-const integer, or to make the existing variable constant in order to make it a valid argument?
bool f( const char s[], const int n )
{
char c[n]; // error: expression must have a constant…

user3650284
- 137
- 1
- 3
- 10
1
vote
4 answers
Array declaration inside a function
Is it safe to write code like below?
void func(int v[], int size) {
int array_local[size];
for(int i = 0; i < size; i++) array_local[i] = v[i];
/*
Other stuff...
*/
}
Or could I stumble upon some kind of error?

user8469759
- 2,522
- 6
- 26
- 50
1
vote
3 answers
Gcc 4.8.2 default compiles and runs variable length arrays fine
In C programming I came across a situation where I accidentally initialized an array with variable size and it worked. I did a bit of research and apparently variable length arrays are available from C99 compilation. Apparently, GCC 4.8.2's default…

user3064869
- 530
- 1
- 6
- 19
1
vote
3 answers
Declared vs non-declared array in C
I came across these two blocks of code:
#include
int main() {
int a[10], i;
for (i = 1; i <= 10; i++) {
scanf("%d", &a[i]);
printf("%d\n", a[i]);
}
return 0
}
When I run the first piece of code, the code runs well but it…

Ankita Runthala
- 11
- 1