The relationship between arrays and pointers in C is a fundamental concept that allows for efficient manipulation of data structures. This program illustrates how arrays and pointers work together and demonstrates their similarities and differences.

Program: Array and Pointer Relationship

Here's a simple C program that shows how to use arrays and pointers to manipulate data:

#include <stdio.h> int main() { int arr[5]; // Declare an array of integers int *ptr; // Declare a pointer to an integer int i; // Initialize the array printf("Enter 5 integers:\n"); for (i = 0; i < 5; i++) { printf("Element %d: ", i + 1); scanf("%d", &arr[i]); // Input values into the array } // Point to the first element of the array ptr = arr; // Display array elements using array indexing printf("\nArray elements using array indexing:\n"); for (i = 0; i < 5; i++) { printf("Element %d: %d\n", i + 1, arr[i]); } // Display array elements using pointer arithmetic printf("\nArray elements using pointer arithmetic:\n"); for (i = 0; i < 5; i++) { printf("Element %d: %d\n", i + 1, *(ptr + i)); // Accessing array elements using pointer } // Modify array elements using pointer printf("\nModifying array elements using pointer:\n"); for (i = 0; i < 5; i++) { *(ptr + i) += 5; // Increase each element by 5 } // Display modified array elements printf("\nModified array elements:\n"); for (i = 0; i < 5; i++) { printf("Element %d: %d\n", i + 1, arr[i]); // Display modified values } return 0; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: This header file is included to use input/output functions like printf and scanf.
  2. Variable Declarations:

    • int arr[5];: This declares an array of 5 integers.
    • int *ptr;: This declares a pointer to an integer.
  3. Input Values:

    • The program prompts the user to enter 5 integers, which are stored in the arr array using a for loop and the scanf function.
  4. Pointer Assignment:

    • The pointer ptr is assigned the address of the first element of the array: ptr = arr;. In C, the name of the array represents the address of its first element.
  5. Display Array Elements Using Array Indexing:

    • A for loop is used to print each element of the array using array indexing.
  6. Display Array Elements Using Pointer Arithmetic:

    • Another for loop is used to access and print each element of the array using pointer arithmetic. Here, *(ptr + i) accesses the element at index i in the array through the pointer.
  7. Modify Array Elements Using Pointer:

    • This section of the code demonstrates how to modify the elements of the array using the pointer. The statement *(ptr + i) += 5; increases each element by 5.
  8. Display Modified Array Elements:

    • Finally, the modified values of the array are printed using the original array indexing method.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code:

    gcc array_pointer_relationship.c -o array_pointer_relationship
  2. Execute the Program:

    ./array_pointer_relationship

Example Output

When you run the program, the output might look like this:

Enter 5 integers: Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 40 Element 5: 50 Array elements using array indexing: Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 40 Element 5: 50 Array elements using pointer arithmetic: Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 40 Element 5: 50 Modifying array elements using pointer: Modified array elements: Element 1: 15 Element 2: 25 Element 3: 35 Element 4: 45 Element 5: 55

Conclusion

This program demonstrates the relationship between arrays and pointers in C. It highlights how arrays can be manipulated using pointers, showing both indexing and pointer arithmetic. Understanding this relationship is crucial for working effectively with data structures in C, enabling more powerful and flexible code.