ProgrammingC Developer

Explain how static arrays are implemented and work in the C language. How to declare them correctly, how do they differ from dynamic arrays, and what nuances are there in initialization and usage?

Pass interviews with Hintsage AI assistant

Answer

Background

In the C language, working with arrays is a fundamental aspect of programming. C provides two main types of arrays: static (whose size is known at compile time and allocated on the stack or in the static/global storage area) and dynamic (whose size is specified at runtime). Static arrays have been used since the creation of the C language, emphasizing performance and simplicity.

Issue

If the differences between static and dynamic arrays are not understood, one may encounter errors: out-of-bounds access, memory leaks, or lifecycle management errors. Improper initialization of static arrays can lead to reading discarded or uninitialized data.

Solution

Declaring a static array looks like this:

int arr[5] = {1, 2, 3, 4, 5};

In this case, the size and contents of the array are defined at compile time, with memory allocated on the stack (for local automatic arrays) or in static memory (if the array is declared as static or global). Special attention should be paid to partial initialization, where unspecified elements are filled with zeros.

The size of a static array cannot be changed during program execution, which distinguishes it from a dynamic one.

Key Features:

  • Immutable size, determined at compile time.
  • Simple declaration and fast operation, guaranteed memory release (upon block completion).
  • Easy and safe initialization, predictable memory management.

Trick Questions.

1. What happens if a static array is not explicitly initialized in a function?

Local automatic (auto) static arrays have uninitialized elements with undefined values. However, if the array is declared with the static modifier or is global, all elements will be initialized to zero by default.

Example:

void foo() { int arr1[3]; // Values are undefined! static int arr2[3]; // All elements are equal to 0 }

2. Can a static array be passed to a function in such a way that the function can change its size?

No, the size of a static array cannot be changed — it is rigidly defined at declaration. The function can work with its contents but not with its size.

Example of correct array passing:

void processArray(int arr[], int size) { arr[0] = 42; }

3. What happens when accessing outside the bounds of a static array? Will a runtime error occur?

No, the C language does not perform checks for out-of-bounds access. Such behavior is considered undefined behavior and can lead to crashes, data corruption, or hidden errors.

Typical Mistakes and Anti-Patterns

  • Using uninitialized local automatic arrays.
  • Out-of-bounds access (buffer overflow).
  • Incorrectly passing the size of the array to functions.

Real-Life Example

Negative Case

A developer declares a local array, forgets to initialize it, and later uses it in a loop. The program produces different results on each run, and sometimes crashes during reading.

Pros:

  • Quick array declaration, no time wasted on initialization understanding.

Cons:

  • High risk of working with garbage values, unpredictable results, difficult debugging.

Positive Case

A developer either explicitly initializes the array with values or declares it as static for automatic zero initialization. Always checks the size and bounds when using the array.

Pros:

  • Deterministic program operation, absence of uninitialized values.

Cons:

  • Size is fixed in advance, inefficient for large dynamic data sets.