Declaring Array in C with variable length [closed]

int n, fibs[n]; attempts to define an array using n for the length, but n has not been initialized, so its value is not determined. Common consequences include:

  • The definition behaves as if n has some small value, possibly zero, and then the following code attempts to store values in the array but overruns the memory reserved for it and thus destroys other data needed by the program.
  • The definition behaves as if n has some large value, causing the stack to overflow and the program to be terminated.

For example, storing 0 to to fibs[0] or 1 to fibs[1] might write to the memory reserved for n. Then the for loop terminates without executing any iterations because the test i < n is false.

Browse More Popular Posts

Leave a Comment