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

Changing the sign of array values in C

I need to write a function that takes the elements in an array and changes the sign (ex. 3 --> -3 or -3 --> 3). l want use this array (int a[2][3] = { { 55,-44,},{1, -4},{6,11} };) instead of ( int a[] = { 5,6,-4};) What should I do? #include…
-1
votes
2 answers

Why can we not use an const int to initialize the size of a char array?

The following code snippet is illegal in C, but works perfectly in C++. Why can we not use a const to help initialize the length of an array in C? #include #include int main () { const int size = 6; char name[size] =…
-1
votes
3 answers

Create C++ variable length global arrays

I would like to create a variable length global array so that I can use them in any functions such as main() or anything. #include int g_t_step[]; int main() { g_t_step[1]=1; std::cout << g_t_step[1]; }
-1
votes
3 answers

Update list using dictionary when values are lists of different lengths

*** UPDATE **** The additional letter "A" was a problem. I'm rephrasing this here. Perhaps clearer? I have to replace values in a list using a dictionary which has lists of variable lengths as its values. For example: variants = {C:["cat", "can",…
-1
votes
1 answer

Segmentation fault 11: while trying to input B[1] using cin

I'm having a problem in running the following code. It is giving me a segmentation fault as a runtime error. #include using namespace std; int main() { int n; cout << "Enter n: "; cin >> n; float A[n][n], x[n], B[n],…
oshhh
  • 141
  • 2
  • 10
-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
  • 4,728
  • 8
  • 44
  • 68
-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,720
  • 2
  • 21
  • 33
-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
  • 75
  • 1
  • 6
-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
  • 769
  • 12
  • 38
-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,261
  • 1
  • 9
  • 19
-2
votes
1 answer

Memory allocation for dynamic structs in C

Say I have a struct struct GRAPH { NODE* nodes[]; } With a dynamic size for nodes[]. I also have struct NODE { char filename[40]; struct NODE* links[]; } I will know how many links and nodes I need at runtime, and can calculate total required…
04Khey
  • 3
  • 1