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

Segfaults on initialization of array of pthread_t

I have a program that takes input n from the command line, and, as part of its operation, tries to call the following code: pthread_t threads[n*n]; Now, for any n <= 1023, this works fine, but the moment I try to use n > 1023, I get a segfault. It…
Koz Ross
  • 3,040
  • 2
  • 24
  • 44
1
vote
1 answer

Initializing a variable length array to zero

int x = 750; int i = 0; while (pow(2, i) < x) { i++; } printf("i is currently %d\n", i); int array[i]; while (i > 0){ i--; printf("The value of array %d is %d\n", i, array[i]); } When I do this, it creates some really crazy values.…
Farion
  • 21
  • 1
  • 3
1
vote
2 answers

Creating legend of variable length datasets+ignoring empty cells

I'm trying to plot some monthly statistics that span over 2 years, where 1 year has only one month of data and the other has 11 months of data. Where I'm having trouble is in the legend label, when I am filling out the label it takes the one month…
user1332577
  • 362
  • 5
  • 17
1
vote
2 answers

How to make a program declare variable number of indefinite size arrays

This might sound crazy but I was wondering if it is possible to make a program declare n number of arrays of the type array[] in a loop using C/C++. For example, sample this pseudo-code: input int _n_ run loop for _n_ times such that: declare…
Quirk
  • 1,305
  • 4
  • 15
  • 29
1
vote
1 answer

Working with arrays and variable length formal parameters. (java:38: error: '.class' expected)

I am not finished working on this program, so I know there may be other mistakes. I am currently getting the '.class' expected error, and for some reason I can't see that mistake. I am using jGrasp and this is a project for my Intro to Java class. …
Nat
  • 37
  • 2
  • 8
1
vote
1 answer

Stack Overflow Behaviour in Native Languages

I'm curious to why most natives languages, including C,C++ and D, doesn't define stack-overflow behaviour. Is it because it would require instrumenting every stack variable allocation and function call which, in turn, would make the code unusably…
1
vote
1 answer

Constant structure with flexible array members

Please consider the following two structures: typedef struct { int num_data; char * name_data; int data[]; } part_t; typedef struct { int num_parts; char * name_parts; part_t parts[]; } container_t; Ideally, I would be…
lupin3
  • 11
  • 2
0
votes
4 answers

C Dynamic Array Size

Hello I am new to coding and was just wondering why the below code: #include int main() { for(int i = 0; 1 ; i++) { char x; char z[1+i]; x=getchar(); if (x == '\n'){ *(z+i) = '\0'; printf("%s",z); …
soda2718
  • 1
  • 2
0
votes
2 answers

C Use an array variable in a function parameter?

I have a 2D array of strings char* d2Array[valueA][valueB]. This works fine, I am able to fill it with strings and access them. There is an issue when passing it to a function. You need to specify the second size parameter (i.e. void aFunction(char*…
0
votes
1 answer

Taking Input an array in C++ using cin directly by operator overloading

#include #include #include template std::istream& operator>>(std::istream& in, T (&arr)[Size]) { std::for_each(std::begin(arr), std::end(arr), [&in](auto& elem) { in >>…
0
votes
2 answers

How do I store integers in an array in c?

I am trying to make a programm that turns a string into binary. int len = strlen(word); int array2[len] = ascii_v(word, len); int ascii_v(string w, int l) { int array1[l]; for (int i = 0, i < l; i++) { array1[i] = word[i]; …
0
votes
0 answers

Values in an Array within a Dynamic Array of Structs being overwritten by previous values

To preface: I am new to C and still learning the best way to efficiently code in C. I'm trying to create an dynamic array of structs. I have an array within the struct however the values within the array are being overwritten by the values that come…
0
votes
3 answers

C How to create an 2d array of characters?

So would like to create an 2D array of characters for testing purposes. Here is my code. const int rows = 4; const int columns = 6; //char field[rows][columns]; //fill_field(rows,columns,field); char field[rows][columns] = { …
0
votes
1 answer

With Construct-Package: RepeatUntil total lenght of subconstruct

Let's say I have: numResults (Int16ul) resultItems[numResults] where the resultItems is constructed like: ID (does not always increase) strLen my_str[strLen] Now I understand, that I have to use RepeatUntil but how the repeathandler is supposed to…
0
votes
1 answer

In C++, why can't I declare an array like this: int array[vector.size()]?

I want to convert a float vector to a float array in C++ 20. I searched online and found this solution: #include #include #include int main() { std::vector input({ 1, 2, 3, 4, 5 }); int…
Pepis
  • 9
  • 1