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
4 answers

ADD elements of an array with a given condition

There is an array of n students( stu[n]).If gender is boy then my code adds for boy b, 2nd,4th,6th,........even position elements of array and for girl g, 1st,3rd,5th....odd position elements of array. 1> Gender of boys denoted by b. 2> Gender…
user11802931
-4
votes
2 answers

Creating array with non constant sizes

I'm currently working on an assignment where I must find a way to output the longest common subsequence of two strings. In all of the other places I have found implementations of this code, they all share one similarity: multiple arrays are…
-4
votes
1 answer

Why variable sized arrays cause Wrong Answer on Codechef?

int n;cin>>n; int arr[n]{}; I have a small problem ,why is this decleration of array wrong?I have used it on Codechef several times until recently i got a WA! After this I declared array as, int n;cin>>n; int arr[1001]{0} ; //max size of input…
-4
votes
2 answers

Where does variable length array/alloca allocate in stack

I am really curious about how alloca() function works and therefore, I have written a simple test program as follows: int test() { int a = 0; int e; char tmp2[a]; //alloca int d; char* tmp3 = new char[2]; tmp2[100] = 1; …
Truong Hua
  • 802
  • 6
  • 18
-4
votes
1 answer

Pitfalls of a variable size array in C

The following snippet compiles properly using standard gcc. What are possible pitfalls here? --especially for kernel level development. int n; f(){n=2;} g(){int b[n];} main(){ int a[n]; f(); g(); }
Ali Abbasinasab
  • 382
  • 3
  • 13
-5
votes
2 answers

Cannot Figure out Why "sizeof(msg) = 0"

I need to find the length of a Message which was entered. I have put all the characters in the msg into an array msg[], but when I check for the size of this array, it comes out to zero. Thank you for your help. #include #define SIZE…
-5
votes
1 answer

Dynamic initialization of 2d array in c++ in a specific way

if we initialize a 2d array in c++ like: int n; cin>>n; int a[n][1000]= {0}; why does it compile but not work properly? I tried to access a[4][2] which gave me 2 while it should give me 0 while in case of: int n; cin>>n; int…
-9
votes
1 answer

length of array at runtime in C++

I have been asked to prove that one can set the array length in the program, and it does not need to be determined at compile time. I have following code: #include using namespace std; int main() { int x, myarray[x]; cout << "Enter…
user3922546
  • 187
  • 1
  • 6
  • 16
1 2 3
27
28