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
4
votes
1 answer

Variable Length Array with length 0?

In C, an array normally isn't allowed to have size 0 (unless I use the one or other compiler-side extension). OTOH, there are VLAs whose length might turn out to be 0. Are they allowed? I am talking about the following code: void send_stuff() { …
glglgl
  • 89,107
  • 13
  • 149
  • 217
4
votes
4 answers

Which compiler should I trust?

This is going to be some what of a newbie question but I was trying to work on a small exercise in the C Language (not C++) and I was running into some issues. Say I wanted to use an array within a method whose size depended on one of the…
4
votes
1 answer

Variable sized arrays in Objective-C?

Okay, so apparently this works: void foo(size_t s) { int myArray[s]; // ... use myArray... } Is this really legal? I mean, it must be, because it compiles (where the C compiler would reject it as non-constant). The first part of my…
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
3
votes
3 answers

Taking sizeof of variable-length array — is there any benefit for doing so?

I am working on a piece of legacy code (no tests). I stumbled upon a section hidden inside several macros. It generates a warning if compiled with GCC's -Wvla. The code in question is equivalent to what can be seen in this small program: typedef…
Grigory Rechistov
  • 2,104
  • 16
  • 25
3
votes
1 answer

Does typedef (pointer to) VLA require evaluation of the size expression?

Does typedef VLA require evaluation of the size expression? int f(void); int main(void) { typedef int (T) [ f() ]; // is f required to be evaluated ? T x; return sizeof x; } Does typedef pointer to VLA require evaluation of the…
pmor
  • 5,392
  • 4
  • 17
  • 36
3
votes
3 answers

Defining a C function that takes a 2D array with variables for dimension sizes

Edit: Turns out the compiler I'm using doesn't support variable length arrays so I have no way of achieving the notation I desire using MSVC I have a function which takes in an array of strings and a query string, and returns the index of the…
3
votes
1 answer

How can I define a pointer to variable length array (VLA) in a struct (or equivalent)?

I declared many pointers to variable length array (VLA) in a function to allocate 2-D arrays dynamically; for example, int M, N; // have some value double (*arr1)[N] = calloc(M, sizeof(double [N])); double (*arr2)[N] = calloc(M, sizeof(double…
Jeongu Kim
  • 117
  • 6
3
votes
4 answers

Why is const int x = 5; not a constant expression in C?

I thought C had no more surprises for me, but this surprised me. const int NUM_FOO = 5; .... int foo[NUM_FOO]; ==>error C2057: expected constant expression My C++ experience has made me internally deprecate #define as much as…
pm100
  • 48,078
  • 23
  • 82
  • 145
3
votes
1 answer

Vue on input value entered, move on to next field

I am new to Vue and it's my first project on Vue as well. I have three input fields which contains on empty values. Is there a way to move on to second input field automatically or make it select, if the input field 1 receives more than 5…
Yahoo
  • 558
  • 11
  • 34
3
votes
1 answer

Segfault after strsep only when compiling with clang 10

I am writing a parser (for NMEA sentences) which splits a string on commas using strsep. When compiled with clang (Apple LLVM version 10.0.1), the code segfaults when splitting a string which has an even number of tokens. When compiled with clang…
3
votes
3 answers

Array Mutation in C

If arrays in C are fixed in size, then how come this code is working properly? This code works but my teacher said I did it in a wrong way... int main() { int n,element,i; printf("Enter the size of Array : "); scanf("%d",&n); int…
user11440137
3
votes
1 answer

sizeof operator in conjunction with variable-length array as function arguments

According to GNU's documentation on Arrays of Variable Length, one can use the sizeof operator for determining the size of a variable length array that was passed into a function: You can also use variable-length arrays as arguments to…
dtracy
  • 33
  • 5
3
votes
2 answers

Find the length of the array that was passed to you in Q#

I have an operation as follows to which the driver needs to send an array of qubits. operation myOp(qubits: Qubit[]) : () { // uses elements from the qubit array } How do I find the length of this array from within the code?
Mahathi Vempati
  • 1,238
  • 1
  • 12
  • 33
3
votes
1 answer

Return a malloc’ed matrix while being able to use subscript notation

I have an exercise where I am supposed to use fixed-size arrays and in/out parameters to do stuff on matrices (add, scanf, print, etc.), but I’d like to do it on arbitrary-length matrices and return them rather than adding each time more (in/)out…
3
votes
3 answers

Initialize array size in C at File Scope

I'd like to initialize an array based on a calculation, but the compiler gives me an error when I try this (I'm using GCC version 6.3.0): const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; char…
dvanaria
  • 6,593
  • 22
  • 62
  • 82