C Programs Tutorials | IT Developer
IT Developer

C Programming - C Pointers and Arrays



Share with a Friend

C Programming - C Pointers and Arrays

C Pointers and Arrays

In C, arrays and pointers are closely related. An array is a collection of elements of the same data type, while a pointer is a variable that stores the memory address of another variable. The relationship between pointers and arrays is fundamental to understanding how memory is managed in C.

Key Concepts:

  1. Array as a Pointer:
    • In most expressions, the name of an array represents the address of the first element of the array.
    • This makes arrays and pointers closely related. In fact, when you pass an array to a function, you are passing a pointer to the first element of the array, not the entire array.
  2. Pointer Arithmetic with Arrays:
    • Pointers can be used to navigate through array elements by using pointer arithmetic.
    • You can increment or decrement pointers to access different elements in an array.

Array Declaration and Pointer Representation:

Array Declaration:

C

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

This defines an array arr of 5 integers.

Pointer Representation:

C

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

Here, arr is treated as a pointer to the first element of the array.

Pointer and Array Relationship:

  1. Array Name as a Pointer:
    • arr is essentially a pointer to the first element of the array arr[0].
    • arr and &arr[0] both give the same memory address.
    • arr[i] can be accessed using the pointer as *(arr + i).
  2. Pointer Arithmetic:
    • In C, pointer arithmetic allows you to traverse an array using a pointer.
    • Incrementing a pointer moves it to the next element in the array (i.e., it adds the size of the data type the pointer is pointing to).

C

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

printf("%d\n", *ptr);       // Prints arr[0] (1)

ptr++;                         // Move pointer to the next element

printf("%d\n", *ptr);       // Prints arr[1] (2)

  1. Accessing Array Elements Using Pointers:
    • arr[i] can be written as *(arr + i), where arr is the address of the first element.
    • Similarly, *(ptr + i) can be used to access array elements when a pointer is used.

Example of Pointer and Array Relationship:

C

#include <stdio.h>

int main() {

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

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

    // Accessing elements using array index

    printf("Array using array notation:\n");

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

        printf("arr[%d] = %d\n", i, arr[i]);

    }

    // Accessing elements using pointer arithmetic

    printf("\nArray using pointer arithmetic:\n");

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

        printf("*(ptr + %d) = %d\n", i, *(ptr + i));

    }

    return 0;

}

Output:

Array using array notation:

arr[0] = 10

arr[1] = 20

arr[2] = 30

arr[3] = 40

arr[4] = 50

Array using pointer arithmetic:

*(ptr + 0) = 10

*(ptr + 1) = 20

*(ptr + 2) = 30

*(ptr + 3) = 40

*(ptr + 4) = 50

Explanation:

  • arr[i] is equivalent to *(arr + i). Both expressions access the i-th element of the array.
  • The pointer ptr is incremented using pointer arithmetic to access each element in the array.

Passing Arrays to Functions Using Pointers:

When you pass an array to a function, you are actually passing the address of the first element of the array, not the entire array. This is done using pointers.

Example:

C

#include <stdio.h>

void printArray(int *ptr, int size) {

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

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

    }

}

int main() {

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

    printArray(arr, 5);  // Passing array to function using pointer

    return 0;

}

Output:

Element 0: 1

Element 1: 2

Element 2: 3

Element 3: 4

Element 4: 5

Explanation:

  • printArray takes a pointer ptr as an argument and uses pointer arithmetic to print the array elements.

Returning Arrays from Functions Using Pointers:

In C, you cannot return an array directly from a function, but you can return a pointer to an array or dynamically allocate memory for an array and return that pointer.

Example (Returning a Pointer to Array):

C

#include <stdio.h>

int* createArray(int size) {

    static int arr[10];  // Static array

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

        arr[i] = i * 10;

    }

    return arr;  // Returning pointer to array

}

int main() {

    int size = 5;

    int *ptr = createArray(size);

    // Printing returned array

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

        printf("%d ", *(ptr + i));

    }

    return 0;

}

Output:

0 10 20 30 40

Explanation:

  • The createArray function returns a pointer to a static array. The function fills the array and returns the pointer to the first element.
  • The array is declared as static so that it persists beyond the function's scope.

Pointer to Array:

In C, you can have a pointer to an entire array, which is different from a pointer to a single element.

Example (Pointer to Array):

C

#include <stdio.h>

int main() {

    int arr[3] = {10, 20, 30};

    int (*ptr)[3] = &arr;  // Pointer to an array of 3 integers

    // Accessing elements using pointer

    printf("arr[0] = %d\n", (*ptr)[0]);

    printf("arr[1] = %d\n", (*ptr)[1]);

    printf("arr[2] = %d\n", (*ptr)[2]);

    return 0;

}

Output:

arr[0] = 10

arr[1] = 20

arr[2] = 30

Explanation:

  • (*ptr)[i] accesses the i-th element of the array arr through the pointer ptr.

Summary:

  • Arrays and pointers are closely related in C. The array name acts as a pointer to the first element of the array.
  • Pointer arithmetic allows easy traversal of array elements.
  • Passing arrays to functions is done via pointers.
  • Returning arrays from functions can be done by returning a pointer to an array or dynamically allocating memory.
  • You can declare pointers to arrays to manipulate entire arrays efficiently.

Understanding the relationship between arrays and pointers is essential for working with arrays, memory, and efficient C programming.