4. C Arrays, Strings & Pointers Deep Dive

Comprehensive Guide: Contiguous array allocation, string null termination, pointer address dereferencing & pointer arithmetic

Module 1: C Arrays & Contiguous Memory Allocation

Storing multiple elements of identical data type in sequential RAM memory blocks.

🇬🇧 English Definition

An array is a fixed-size sequential collection of elements of the same data type stored in contiguous memory locations.

🇮🇳 Hinglish Definition

Array ek hi type ke multiple values ko line se (contiguous memory me) store karne ka tarika hai. Indices 0 se shuru hote hai.

Core Concepts & Rules

  • 0-indexed access: array[0] is the first element.
  • Contiguous Memory: Address of element i = Base Address + (i * size_of_datatype).
  • Multi-dimensional arrays (2D arrays) represent rows and columns.

🔍 Line-by-Line Code Breakdown

  1. Line 2: Declare and initialize integer array nums[4] = {10, 20, 30, 40}.
  2. Line 3-5: for loop iterating indices 0 to 3 printing values.
💻 Practical C Code Example - C Arrays & Contiguous Memory Allocation
int main() {
    int nums[4] = {10, 20, 30, 40};
    int i;
    for (i = 0; i < 4; i++) {
        printf("Element at index %d: %d\n", i, nums[i]);
    }
    return 0;
}
📤 Compiled Program Output:
Element at index 0: 10 Element at index 1: 20 Element at index 2: 30 Element at index 3: 40

Module 2: C Pointers, Addresses & Dereferencing

Directly reading and manipulating raw RAM memory addresses.

🇬🇧 English Definition

A pointer is a variable that stores the memory address of another variable as its value.

🇮🇳 Hinglish Definition

Pointer ek aisi special variable hoti hai jo kisi doosre variable ka RAM Address (Memory Location) store karti hai.

Core Concepts & Rules

  • Address-of Operator (&): Returns memory location address of variable.
  • Dereference Operator (*): Accesses value stored at the memory location.
  • Pointer Type MUST match variable data type (int* for int).

🔍 Line-by-Line Code Breakdown

  1. Line 2: Initialize int val = 100.
  2. Line 3: Declare pointer int *ptr = &val storing RAM address of val.
  3. Line 4: Prints stored value of val.
  4. Line 5: Prints dereferenced value *ptr (which accesses val -> 100).
💻 Practical C Code Example - C Pointers, Addresses & Dereferencing
int main() {
    int val = 100;
    int *ptr = &val;

    printf("Value of val: %d\n", val);
    printf("Value via Pointer (*ptr): %d\n", *ptr);
    return 0;
}
📤 Compiled Program Output:
Value of val: 100 Value via Pointer (*ptr): 100