C Arrays and Pointers

#include <stdio.h>

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int *ptr = numbers;

    printf("First element: %d\n", numbers[0]);
    printf("First element using pointer: %d\n", *ptr);

    ptr++;
    printf("Second element using pointer: %d\n", *ptr);

    ptr = ptr + 2;
    printf("Fourth element using pointer: %d\n", *ptr);

    return 0;
}

Explanation: 1. #include <stdio.h>: Includes the standard input-output library.

  1. int main() {: Begins the main function.

  2. int numbers[] = {1, 2, 3, 4, 5};: Declares an array named numbers containing five integers: 1, 2, 3, 4, and 5.

  3. int *ptr = numbers;: Declares a pointer ptr of type integer and assigns it the address of the first element of the array numbers.

  4. printf("First element: %d\n", numbers[0]);: Prints the value of the first element of the array numbers.

  5. printf("First element using pointer: %d\n", *ptr);: Prints the value pointed to by ptr, which is the first element of the array numbers.

  6. ptr++;: Increments the pointer ptr to point to the next element in the array.

  7. printf("Second element using pointer: %d\n", *ptr);: Prints the value pointed to by the incremented ptr, which is the second element of the array numbers.

  8. ptr = ptr + 2;: Moves the pointer ptr two positions ahead from its current position in the array.

  9. printf("Fourth element using pointer: %d\n", *ptr);: Prints the value pointed to by the updated ptr, which is the fourth element of the array numbers.

  10. return 0;: Indicates successful termination of the main function.