Fixed-Length Array

======================

A Fixed-Length Array (also known as an arrays with fixed length) is an Array data structure that has a specific number of elements and a constant size. This means that the number of elements in the Array must be known at compile-time, whereas Dynamic Arrays can grow or shrink as needed.

Syntax


The syntax for creating a Fixed-Length Array is similar to that of Dynamic Arrays:

arr [start_index] [length]
  • arr is the name given to the Array.
  • [start_index] specifies the starting index of the Array. This must be an integer value and can range from 0 to the length minus 1.
  • [length] specifies the number of elements in the Array. This must be a non-negative integer.

Example


Here’s an example of creating a Fixed-Length Array:

int numbers[5];

In this example, numbers is an Array with a length of 5 and a starting index of 0. You can initialize it with values like numbers[0] = 10, numbers[1] = 20, etc.

Advantages


Fixed-length arrays have several advantages:

Disadvantages


Fixed-length arrays also have some disadvantages:

  • Compile-time checks: To prevent errors, the size of the Array must be known at compile-time. This can make it more difficult to write code that works with dynamically sized data structures.
  • No Resizing: If you need to add or remove elements from a Fixed-Length Array, you’ll have to manually adjust its size.

Usage


Fixed-length arrays are commonly used in situations where the size of the data is known at compile-time. Here are some examples:

Array of integers

Suppose we want to write a program that generates a sequence of random numbers and stores them in an Array:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int length;
    printf("Enter the number of elements: ");
    scanf("%d", &length);

    int *numbers = (int *)malloc(length * sizeof(int));

    // ... Generate random numbers and store them in the [Array](/Array) ...

    for (int i = 0; i < length; i++) {
        printf("Number %d: %d\n", i + 1, numbers[i]);
    }

    free(numbers);
    return 0;
}

Array of strings

Suppose we want to write a program that reads input from the user and stores it in an Array:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int length;
    printf("Enter the number of elements: ");
    scanf("%d", &length);

    char **strings = (char **)malloc(length * sizeof(char *));
    char *input;

    // ... Read input from the user and store it in an [Array](/Array) ...

    for (int i = 0; i < length; i++) {
        printf("String %d: %s\n", i + 1, strings[i]);
    }

    free(strings);
    return 0;
}

Example Use Case


Here’s a complete example of using a Fixed-Length Array in C:

#include <stdio.h>
#include <stdlib.h>

// Define the size of the [Array](/Array) and its starting index
#define ARRAY_SIZE 5

int main() {
    int numbers[ARRAY_SIZE];

    // Initialize the [Array](/Array) with random values
    for (int i = 0; i < ARRAY_SIZE; i++) {
        numbers[i] = rand() % 100;
    }

    printf("Original [Array](/Array): ");
    for (int i = 0; i < ARRAY_SIZE; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n");

    // Print the [Array](/Array) with fixed length
    printf("[Array](/Array) with Fixed Length: ");
    for (int i = 0; i < ARRAY_SIZE; i++) {
        printf("%d ", numbers[i % ARRAY_SIZE]); // Use modulus to cycle through the [Array](/Array)
    }
    printf("\n");

    return 0;
}

This example creates a Fixed-Length Array numbers with a size of 5 and an initial value of 0. It then prints both arrays using different methods.