C Programs Tutorials | IT Developer
IT Developer

C Programming - C Pointer to an Array



Share with a Friend

C Programming - C Pointer to an Array

C Pointer to an Array

In C, you can create a pointer that points to the first element of an array. Understanding pointers to arrays is important because it enables efficient manipulation of array elements, passing arrays to functions, and performing pointer arithmetic.

An array name in C is essentially a pointer to the first element of the array, so understanding how pointers and arrays work together is crucial for efficient programming.

Declaration of Pointer to an Array

To declare a pointer that points to an array, you would declare it as:

C

type (*pointer_name)[array_size];

  • type: The data type of the array elements.
  • pointer_name: The name of the pointer variable.
  • array_size: The size of the array.

This syntax means that pointer_name will point to an array of array_size elements of type type.

Example 1: Pointer to an Array

C

#include <stdio.h>

int main() {

    int arr[5] = {1, 2, 3, 4, 5};

    // Declare a pointer to the array

    int (*ptr)[5];

    // Point to the entire array 'arr'

    ptr = &arr;

    // Accessing array elements through the pointer

    printf("Element 0: %d\n", (*ptr)[0]);

    printf("Element 1: %d\n", (*ptr)[1]);

    printf("Element 2: %d\n", (*ptr)[2]);

    printf("Element 3: %d\n", (*ptr)[3]);

    printf("Element 4: %d\n", (*ptr)[4]);

    return 0;

}

Output:

Element 0: 1

Element 1: 2

Element 2: 3

Element 3: 4

Element 4: 5

In this example:

  • ptr is declared as a pointer to an array of 5 integers (int (*ptr)[5]).
  • We assign ptr to the address of the array arr.
  • To access elements of the array, we dereference the pointer and use the array index, like (*ptr)[index].

Example 2: Pointer to an Array as a Function Argument

When passing an array to a function, you can use a pointer to an array. This allows the function to manipulate the array directly.

C

#include <stdio.h>

// Function that takes a pointer to an array of 5 integers

void printArray(int (*arr)[5]) {

    for (int i = 0; i < 5; i++) {

        printf("%d ", (*arr)[i]);

    }

    printf("\n");

}

int main() {

    int arr[5] = {10, 20, 30, 40, 50};

    // Passing pointer to the array

    printArray(&arr);

    return 0;

}

Output:

10 20 30 40 50

In this example:

  • The function printArray accepts a pointer to an array of 5 integers (int (*arr)[5]).
  • We pass the address of the array arr (&arr) to the function.
  • Inside the function, the array elements are accessed through the pointer by dereferencing it ((*arr)[i]).

Example 3: Pointer Arithmetic with Array

Since an array name is essentially a pointer to its first element, pointer arithmetic can be used to iterate through the array.

C

#include <stdio.h>

int main() {

    int arr[5] = {1, 2, 3, 4, 5};

    int *ptr = arr;  // Pointer to the first element of the array

    // Using pointer arithmetic to access elements

    for (int i = 0; i < 5; i++) {

        printf("Element %d: %d\n", i, *(ptr + i));  // Accessing using pointer arithmetic

    }

    return 0;

}

Output:

Element 0: 1

Element 1: 2

Element 2: 3

Element 3: 4

Element 4: 5

In this example:

  • The pointer ptr is assigned the address of the first element of the array arr.
  • We then use pointer arithmetic (*(ptr + i)) to access each element of the array.
  • The ptr + i gives the address of the ith element, and * dereferences it to get the value.

Key Points:

  1. Array Name as a Pointer: In C, the name of an array is a constant pointer to the first element, so arr can be used as a pointer.
  2. Pointer to an Array: To declare a pointer to an entire array, the syntax is type (*ptr)[size], where ptr points to an array of size elements of type type.
  3. Pointer Arithmetic: You can use pointer arithmetic to navigate through the elements of an array. Adding an integer to a pointer moves it by that many elements, not bytes.

Conclusion:

A pointer to an array is useful when you need to work with the entire array in a function or when dealing with large data structures. Understanding how pointers and arrays work together in C helps in writing efficient code, especially when working with multi-dimensional arrays, dynamic memory allocation, or performing pointer arithmetic.