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
5 answers
Is it possible to declare a global 2D array in C/C++?
When I try to declare a global two-dimensional array in C++ like so:
int maxX = 10;
int maxZ = 10;
SDL_Rect mapX[maxX][maxZ];
I get an error that says error: variable-size type declared outside of any function

Shasaur Aura
- 65
- 1
- 1
- 9
1
vote
3 answers
Array using functions on C
A program to accept an array and diplay it on the console using functions.
Program should contain 3 functions including main() function.
Next two functions for accepting values to the array, and display array values on the console…

Amalshanth
- 29
- 1
- 7
1
vote
2 answers
How do I store the sums of the elements in each row of a 2d array in 1d array with a function?
I have the following task: Create a 2d array A[M][N], where M and N are inputted by the user as well the elements in each row and column. There must also be two functions:
1)The first one needs to calculate the average of all positive numbers in the…

Rostislav Yanev
- 11
- 1
1
vote
1 answer
Error when dynamically allocating a 2D array - ISO C90 forbids variable length (C89/C90)
I'm trying to program up a 2D dynamically allocated array for a map for a small game. Below is my code, I'm unsure why int* array[yCoord] is throwing up the error that it cannot be a variable length array, as the variable yCoord is assigned a…

tomparko
- 43
- 6
1
vote
2 answers
struct with variable length array in std::variant
So I'm working with this struct type with variable length array member, like this:
struct Entry;
struct Data {
int members;
size_t entries_size;
Entry entries[1];
};
The entries_size member is the actual size of the entires array, it…

fluter
- 13,238
- 8
- 62
- 100
1
vote
3 answers
C++ Fibonacci number generator not working. Why?
I just wanna know why does this method to get the fibonacci number not work thanks.
#include
#include
#include
using namespace std;
int fibonacci()
{
cout << "enter sequence num: " << endl;
int num;
cin >>…

Big Yong
- 23
- 3
1
vote
3 answers
How to create an n sized array in c
I am very new to C but I am having trouble on what seems a very trivial problem. All I am trying to do is create a n sized array, such that at the time of running I don't know its size and can't specify it. The following code works perfectly…

Harry Spratt
- 179
- 11
1
vote
3 answers
4 Dimensional Array - C
What is the problem with the following code? C compiler shows me error: Segmentation fault.
#include
#include
int main() {
int n = 4;
float A[n][n][n][n];
A[n][n][1][1] = 1;
A[n][n][1][2] = 0;
A[n][n][1][3] =…

Luka Kaličanin
- 13
- 2
1
vote
1 answer
is it safe to make a VLA slightly longer than argv[1]?
Example code, treats argv[1] as a file path, replacing its extension with .png or appending it if none is found:
#include
#include
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *lastdot =…

landfill baby
- 39
- 7
1
vote
3 answers
C: Constant length doesn't allow me to declare array
I'm experiencing this annoying issue when I try to compile a simple code to understand pointers.
Basically, the error happens in the declaration of the array:
// Use of pointers
#include
int main(void) {
const int SIZE = 5;
int…

Nicolás Rivera
- 70
- 8
1
vote
1 answer
Clang padding array
I am trying to understand how function calling works at machine level. For that I created a dummy C++ function like this :
int func(int a[], int n) {
char arr[n];
arr[n - 99] = 100; //to prevent compiler from optimizing my function to noop
…

driewguy
- 35
- 3
1
vote
2 answers
The value of integer is changing without any calculation
I have no idea why suddenly the value of integer is changing even though there's no process of caluclation, here's the code of my program :
#include
using namespace std;
int main()
{
int n;
int a[] = {};
int b[] = {};
…

Yasamura
- 41
- 3
1
vote
2 answers
How do I tell if I am using VLA (Variable Length Array)?
I am on a project where we have to read in from a file, temporarily store them in dynamically allocated memory, do sorting and stuff, and deallocate the memory.
As per the project is testing our knowledge over dynamic memory and memory leak, one of…

ThomTheGreat
- 13
- 3
1
vote
3 answers
Static array size vs array size through input in C
I was trying to find out the reason why arrays have static size in C, and so far I know that dynamic allocation can impact how long it takes for code to execute. Also, I know that another reason would be that its size needed to be fixed at compile…

Game changer
- 41
- 6
1
vote
1 answer
Why is this C array giving the "Expected Expression" error?
learning the C programming language and reading the documentation with Xcode 13.2 open and a Command-Line Tool project in front of me.
Reading this, in the Declarations/Arrays/Variable Length Arrays section:
{
int n = 1;
label:
int a[n]; //…

NotationMaster
- 390
- 3
- 17