> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C One-Dimensional Array

A one-dimensional array in C is a contiguous block of memory designed to store a fixed-size sequence of elements sharing a single data type. The array identifier represents the array object itself (of type `T[N]`). It is not a pointer or a reference; rather, during most expression evaluations, the identifier implicitly decays into a pointer to its first element.

## Declaration Syntax

To declare a one-dimensional array, specify the data type, the array identifier, and the total number of elements enclosed in square brackets `[]`.

```c theme={"dark"}
data_type array_name[array_size];
```

* **`data_type`**: Determines the amount of memory allocated per element and how the bit patterns are interpreted (e.g., `int`, `char`, `float`).
* **`array_size`**: Must be an integer constant expression greater than zero (excluding C99 Variable Length Arrays). This dictates the total memory footprint: `array_size * sizeof(data_type)`.

## Initialization

Arrays can be initialized at the point of declaration using an initializer list enclosed in braces `{}`.

```c theme={"dark"}
// 1. Full initialization
int arr1[5] = {10, 20, 30, 40, 50};

// 2. Implicit size derivation (compiler infers size as 4)
int arr2[] = {10, 20, 30, 40};

// 3. Partial initialization (remaining elements are zero-initialized)
int arr3[5] = {10, 20}; // Results in: {10, 20, 0, 0, 0}

// 4. Designated initializers (C99 standard)
int arr4[5] = {[1] = 10, [4] = 50}; // Results in: {0, 10, 0, 0, 50}
```

## Memory Layout and Indexing

C arrays are zero-indexed. The index represents the offset from the base address. Because the memory is strictly contiguous, the compiler calculates the exact memory address of any element in constant time $O(1)$ using pointer arithmetic:

`Address of array[i] = Base_Address + (i * sizeof(data_type))`

```c theme={"dark"}
int arr[3] = {10, 20, 30};

/* 
Assuming base address is 0x1000 and sizeof(int) is 4 bytes:
arr[0] -> 0x1000 (Value: 10)
arr[1] -> 0x1004 (Value: 20)
arr[2] -> 0x1008 (Value: 30)
*/
```

## Access and Mutation

Elements are accessed and mutated using the subscript operator `[]`. Under the hood, `array[i]` is syntactic sugar for dereferencing a pointer offset: `*(array + i)`.

```c theme={"dark"}
int arr[3] = {0};

// Mutation
arr[0] = 15;
arr[1] = 25;

// Access
int val = arr[1]; // val is now 25
```

## Array-to-Pointer Decay

In most expressions, a one-dimensional array identifier "decays" into a pointer to its first element (`&array[0]`).

```c theme={"dark"}
int arr[5];
int *ptr = arr; // arr decays to int*, equivalent to ptr = &arr[0]
```

**Exceptions to Decay:**
The array does *not* decay to a pointer in three specific contexts:

1. When it is the operand of the `sizeof` operator (returns the total byte size of the array object).
2. When it is the operand of the `&` (address-of) operator (returns a pointer to the entire array, e.g., `int (*)[5]`).
3. When it is a string literal used to initialize a character array.

## Bounds Checking

The C standard dictates that there is **no implicit bounds checking** on array accesses.

```c theme={"dark"}
int arr[5];
arr[10] = 100; // Compiles successfully, but causes Undefined Behavior (UB)
```

Writing to or reading from an index outside the range of `0` to `array_size - 1` results in Undefined Behavior, which typically manifests as memory corruption, segmentation faults, or silent data mutation in adjacent memory blocks.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
