Questions tagged [variable-length-array]

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.

413 questions
2
votes
3 answers

How to find the position of an item of a list in python?

I have two lists. The first list has a specific length and the second list has unstable length. I want to assign the items from two lists. I use colour_index = sel_pos%(len(colours)). The sel_pos is the item's position from the unstable list. How to…
user6584717
2
votes
2 answers

Initializing structure with variable length array in C

Does anyone know if there is a way to initialize a structure containing a variable length array without initializing the array first in a separate variable (and without using malloc)? My structure looks like this: struct my_struct { int…
FClad
  • 21
  • 2
2
votes
2 answers

how to put numbers from interval to array? (C language)

I am looking for this for a while. Can anyone tell me how can I create interval array? Example: interval = < 4;9 > int array[9-4+1] = {4,5,6,7,8,9} I would like to insert number from interval to array and than I can work with values in…
2
votes
2 answers

Function that creates and returns an array is causing problems

I tried to write a function that returns a random array of pixel colors, so when I call randomPalette(i), the function will create a random array of i colors. Below are my codes. It says error at random[colors] that expression must have constant…
MLAC
  • 129
  • 2
  • 10
2
votes
4 answers

Return VLA and usage

I have the following function: int* create_matrix_2(int rows, int cols) { double (*A)[rows][cols] = malloc(sizeof(int[rows][cols])); for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { …
Zelphir Kaltstahl
  • 5,722
  • 10
  • 57
  • 86
2
votes
2 answers

Unexpected compiler behavior when declaring array of variable size

So, I was teaching base C programming to a student for a test. Talking about array declaration, I told him: "you can do this" int myArray[10]; -> show him that the code compiles "you can do this, too" #define ARRAY_SIZE 10 [...] int…
il_mix
  • 553
  • 8
  • 20
2
votes
1 answer

Code:Blocks Mingw Compiler Error: Variable-Sized Object May Not Be Initialized

I am creating a simple terminal fantasy game using C++. I have seemed to run into an error "error: variable-sized object 'items' may not be initialized". Here is the code: string useItem(int item) { string items[item] = {"HP Potion","Attack…
2
votes
2 answers

Effective size versus actual size of an array, how to get to know the effective size?

I guess an other way to ask the same question is how to know the number of null pointing elements in an array? int[] arrayOfEffectiveSizeTen = new int[10]; // some black box in which the content of the array is…
Joop
  • 3,706
  • 34
  • 55
2
votes
4 answers

Passing unknown Array to Function by reference (C++)

I have spent a good hour trying to figure this out - how do I write this function (at top of code - insertionSort) that allows me to pass an array by reference to it. In a way that allows me to call '.size' on the array. It has to be an array for…
2
votes
1 answer

Visual Studio 2013 won't compile C array declaration

I've read multiple posts about this issue and have not been able to solve my problem. I have a file with a .c extension that compiles perfectly on gcc. I've made sure that in the project properties it is set to Compile to C code. Still, when I try…
2
votes
3 answers

Which of these two methods is the correct way to give dimensions to a static array?

Are these two forms of array declaration correct? First: int n; n=3; int A[n]; Second: #define N 300; . . . . int a[N]; For me the two ways are totally valid, but someone told me the first is wrong. The first one is like saying: int A[10]; So,…
exsnake
  • 1,767
  • 2
  • 23
  • 44
2
votes
1 answer

dynamic and static memory allocation?

int x; cin>>x; int arr[x]; The code must not compile because the program will try allocate a unknown memory for the array on the stack, BUT IT COMPILES! i know what dynamic memory is, i've read a lot about this but i don't understand ,…
user3927214
  • 67
  • 2
  • 3
  • 9
2
votes
2 answers

Are variable length arrays supported under std=gnu89?

My code contains the following line: int counts[config.max_c]; I was surprised to see that it compiled without issue using default gcc without any flags. According to man gcc the default standard for c code is gnu89. Does this standard support this…
Flash
  • 15,945
  • 13
  • 70
  • 98
2
votes
6 answers

How to pass a VLA to a function template?

I have the following code which could not be complied. using namespace std; void f(int); template void array_ini_1d(T1 (&x)[N]) { for (int i = 0; i < N; i++) { x[i] = 0; } } What is the proper way to pass the array…
2
votes
2 answers

C++ what is the difference between static and dynamic allocation of this array?

int length = 5; int hi[length]; vs int length = 5; int *hi = new int[length]; I was taught compilers complain in C whenever you try to statically allocate arrays w/ size that's not constant. So if you ever needed array of unknown size, you needed…
Popcorn
  • 5,188
  • 12
  • 54
  • 87