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
2
votes
2 answers
How to return a 2D array?
I was searching for an efficient way to create a 2D array and return it, usually I make an array of pointers, allocate memory for each pointer, and return an **ptr. I was looking for another way to do it because then I have to free every single…

Joao Lima
- 87
- 6
2
votes
1 answer
can we give the number of elements in an array as needed using a variable?
#include
int main() {
int n;
scanf("%d", &n);
int L[n];
// ...
return 0;
}
I'm asking if I can give the number of elements in an array as needed using a variable . The teacher told us that this method is not…

TheKing
- 136
- 9
2
votes
2 answers
Is variably-modified type a VLA only?
A simple question: is variably-modified type a VLA (variable length array) only?
C11, 6.10.8.3 Conditional feature macros, 1 (emphasis added):
__STDC_NO_VLA__ The integer constant 1, intended to indicate that the
implementation does not support…

pmor
- 5,392
- 4
- 17
- 36
2
votes
1 answer
Implementing a 3D array as a pointer to a variable length array
I just learned about variable length arrays in C, and somebody suggested to me that I can realize a 3D array-like object in C simply as a pointer to a variable length array. A simple implementation would look as follows:
#include
#include…

Alf
- 1,821
- 3
- 30
- 48
2
votes
2 answers
A pointer-to-a-VLA-typed controlling expression in a _Generic expression
Why doesn't this static assertion trigger on major compilers?
void test(int x){
_Static_assert(_Generic( (char(*)[x])0,
char (*)[1]: 1, default: 0),"");
}
https://gcc.godbolt.org/z/E67a79oPT
Are pointers to variable-length-arrays supposed…

Petr Skocik
- 58,047
- 6
- 95
- 142
2
votes
3 answers
Adding two corresponding array elements and return the resultant array
Facing segmentation fault in the following code . Can anyone help me fix it?
#include
int* multiply(int *arr1, int *arr2, int m);
int main(void){
int m = 0;
printf("Enter size of array1 and array2 >");
…

Suraj Panda
- 23
- 5
2
votes
3 answers
Fibonacci number code with for-loop and if statement error
so i'm a beginner and trying to solve a small project about making fibonacci number.
The code essentially is about typing n (the sequence) and it will show you what value of fibonacci number in that n-sequence.
but of course the code went wrong, the…

imnotarobot
- 59
- 1
- 4
2
votes
2 answers
Why sizeof (array A[n]) without n defined in C++is fixed?
When I try to find the sizeof(A) where A is of type int with size as 'n', n is an int that is not defined. I get an output of 496 and when I give a value to n and then check it, sizeof(A) gives me the same values of 496.
I know Array is a static…

Itzz_CJ
- 70
- 5
2
votes
1 answer
c++ - no matching function for call to
My terminal messages
zo@laptop:~/Desktop$ g++ stack.cpp
stack.cpp: In function ‘int main(int, char**)’:
stack.cpp:50:19: error: no matching function for call to ‘boyInitial(Boy [boyNumber])’
boyInitial(boy);
…

Zohar-fzh
- 71
- 7
2
votes
0 answers
Multi dimensional array as VLA function parameter with explicit size
Context
I am working on a computational fluid dynamics code where I work with vector and matrices. Consequently I want to pass them to functions every once in a while.
The memory for vectors and matrices is allocated as follows consequently…

koipond
- 306
- 2
- 8
2
votes
2 answers
Are unconstrained arrays in Ada safe to use?
I was reading up on array types in Ada and found it interesting that, unlike C++, the language allows their size to be unknown at compile time. I was not sure how they are implemented though so I wrote a small test:
with Ada.Text_IO; use…

GDI512
- 183
- 1
- 9
2
votes
1 answer
Understanding the handling Variable-Length Data with special focus on variable length arrays in C(99)
Below is an excerpt from the red dragon book. It deals with the handling of variable length data items in the activation record of the procedure.
Pascal is almost unique among languages in requiring that arrays local to a procedure have a length…

Abhishek Ghosh
- 597
- 7
- 18
2
votes
3 answers
Is this a valid way to declare an array in C?
I know you're not supposed to declare arrays with a variable as the size ex. int arr[n]; because the size of an array is supposed to be static if not using dynamic memory but what about if you have a function like this? Would this be valid or no? It…

g3or3
- 63
- 4
2
votes
1 answer
Facing problem while initializing an array
I am facing a problem while initializing an array in C. I am trying to take input of a variable 'n' and declare an array marks[n] with its value set to zero. I wrote the following part of the program to do it.
int n,k,e,m,x;
scanf("%d %d %d %d",…

Miraz
- 343
- 3
- 15
2
votes
3 answers
Simple but specific array compiling error (C)
This is what I write:
const int MAX=100;
int main (){
int notas [MAX]={0};
The compiler says the following:
[Error] variable-sized object may not be initialized
[Warning] excess elements in array initializer
When I write MAX with…

Gabriel9721
- 31
- 4