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.

329 questions
-1
votes
1 answer

I need to create an n-element array a where each index i has a reference to another array of K integers

The value of K varies from array to array. How to do this? This is what I have to achieve. Its the hackerrank variable sizedarray question. https://s3.amazonaws.com/hr-challenge-images/14507/1476906485-2c93045320-variable-length-arrays.png I've…
-1
votes
1 answer

Why does this C code (reading a file) crash upon exit (or file reallocation)?

The following code crashes right before upon program exit. I have tested it on both MSVS 2015 and GCC. The program is just assigning a VLA on the heap (read about it here if you want) and reads a file contents character by character and storing this…
KeyC0de
  • 3,375
  • 5
  • 35
  • 50
-1
votes
3 answers

Why is the seventh variable 6 in my program?

#include int main(void) { int n,i; int str[n]; scanf("%d\n",&n); for(i=0;i<=n;i++) { scanf("%d",&str[i]); printf("%d th %d\n",i,n); } return 0; } Input: 10 8 9 2 1 4 10 7 6 8 7 Output: 0 th…
-1
votes
1 answer

TypeError: cannot read property 'length' of undefined (defined by function parameter)

I'm trying to make the simple code below works, but always got the following error: TypeError: cannot read property 'length' of undefined. function multiplyAll(arr) { var product = 1; if (arr === undefined) { return "Undefined…
-1
votes
3 answers

If len Statement

I type in seven digits to be able to work out a gtin-8 product code. But if I type in more than seven digits, the if len statement is meant to recognise that I have typed in more than seven digits, but it doesn't. I tried putting this into an…
Lewis Graham
  • 13
  • 1
  • 3
-1
votes
2 answers

How to fix this function so that it returns concatenation of two strings?

I am trying to write a custom function in C that will concatenate two strings. So far, I have come up with: char *strcat406(char *str1, char *str2) { int str1length = 0; int str2length = 0; char newStr[str1length + str2length]; int…
Omar N
  • 1,700
  • 2
  • 16
  • 28
-1
votes
4 answers

Is it necessary to use new for dynamic memory allocation?

In C, we can input the size of an array (at runtime) from the user by the concept of dynamic memory allocation. But we can also use int n; scanf("%d",&n); int a[n]; So what is the need of using pointers for dynamic memory allocation using new?
Sahil Julka
  • 15
  • 1
  • 4
-1
votes
2 answers

C99 - Why can't I use a variable-length char array in Xcode 6?

Got a C Command-Line tool project going in Xcode 6, and everything works great except one little thing: I can't for the life of me figure out how to assign a value to a variable-length array! For example, consider the following code: #include…
Kenny83
  • 519
  • 4
  • 20
-1
votes
1 answer

Static array in C

I always knew that it was not possible to build a dynamic array in C without using malloc and free, so why is this code compiling and running correctly? #include #include int main() { int a; printf("Insert a number:…
woggioni
  • 1,036
  • 1
  • 8
  • 16
-2
votes
1 answer

variable char size without malloc in C

does char myStr[varLength] work in C? I have the following (code here): int isVMin(char c){ return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y'; } int…
serge
  • 9,891
  • 17
  • 84
  • 142
-2
votes
2 answers

I want to convert Vararg Integer to Vararg int?

The below code is working fine using Integer values for my variadic method. But I want to use "int" instead of "Integer". Here is the code: package JavaPracticeShuffler; import java.util.ArrayList; import java.util.List; import…
-2
votes
1 answer

Write a program that checks if a 2-D integer array is a square array, meaning, if its rows and columns are equal. My code is below

package Homeworks; public class HomeWork85 { public static void main(String[] args) { int[][] a = { {1,1,1,2}, {1,1,1}, {1,1,1} }; int[][] b = { {1,1,1,1}, {1,1,1,1}, …
ozkan cil
  • 3
  • 2
-2
votes
2 answers

ArrayIndexOutOfBoundsException on array access

Good afternoon! I have to create 2 arrays. 1st one should create a random number (-10;10) if a user write 0, otherwise it should count using an entered formula; the 2nd array should write firstly elements from an array1 which have uneven number of…
Eli
  • 7
  • 1
-2
votes
1 answer

Passing pointer and then allocating variable length array to stack

Is it possible to allocate a variable length array to the stack in one function from another function? One way that works is to just allocate the largest possible size up front, but I'm wondering if there is a way to avoid this. void…
-2
votes
2 answers

array size initialized dynamically example

I was having trouble understanding why int n; cin>>n; int arr[n]; works. I was told that this code should not run because the value of 'n' could only be declared during runtime and therefore should not compile. I was also told that my 'n'…
coder666
  • 41
  • 6
1 2 3
21
22