C pointers and arrays

Arrays and pointers are closely related in C. In fact, the name of an array is essentially a pointer to the first element of that array. When you use the name of an array (like arr), it represents a pointer to the first element of that array.

Example:


#include <stdio.h>


int main() {
    int arr[5] = {10, 20, 30, 40, 50};   // Array with 5 elements
    int *ptr = arr;              // Pointer points to the first element of the array
    printf("First element: %d\n", *ptr);    // Output: 10 

    return 0;
}

Output:

First element: 10

Explanation:

  1. arr points to the first element of the array, arr[0].
  2. ptr is a pointer that holds the address of arr[0], so dereferencing ptr gives you the value 10.