Skip to content

Pointers and Arrays

Pointers and arrays are closely related concepts in C programming and understanding their relationship is crucial for efficient memory management and array manipulation. Let’s explore pointers and arrays comprehensively:

1. Pointers and Arrays:

  • In C, arrays are contiguous blocks of memory that store elements of the same data type.
  • Arrays are accessed using indices, which represent the offset from the beginning of the array.
  • Pointers, on the other hand, store memory addresses. They point to the location of a variable or data structure in memory.

2. Relationship between Pointers and Arrays:

  • An array name behaves like a constant pointer pointing to the first element of the array.
  • Therefore, when you use the array name in an expression (except when it is the operand of the sizeof operator or is a string literal being used to initialize another array in a declaration), it gets converted to a pointer to its first element.

3. Initializing Pointers with Arrays:

  • Pointers can be initialized with the address of the first element of an array.

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

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

4. Accessing Array Elements through Pointers:

  • Array elements can be accessed through pointers using pointer arithmetic or dereferencing.

printf(“%d\n”, *(ptr + 2));  // Output: 3 (Accessing third element of ‘arr’ through ‘ptr’)

printf(“%d\n”, ptr[2]);      // Output: 3 (Another way to access third element of ‘arr’ through ‘ptr’)

5. Pointer Arithmetic and Arrays:

  • Pointer arithmetic allows you to navigate through array elements efficiently.

int *ptr = arr;

printf(“%d\n”, *ptr);      // Output: 1 (First element of ‘arr’)

printf(“%d\n”, *(ptr+1));  // Output: 2 (Second element of ‘arr’)

6. Passing Arrays to Functions:

  • Arrays are typically passed to functions by passing a pointer to the first element of the array.
  • Since arrays decay to pointers when passed to functions, changes made to array elements within the function are reflected outside the function.

void modifyArray(int *arr, int size) {

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

        arr[i] *= 2;   // Double each element of the array

    }

}

int main() {

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

    modifyArray(arr, 5);  // Pass array ‘arr’ to function

    // Array ‘arr’ is modified by the function

    return 0;

}

7. Multi-dimensional Arrays and Pointers:

  • Multi-dimensional arrays are stored as contiguous blocks of memory, allowing pointers to be used for accessing elements efficiently.

int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int (*ptr)[3] = arr;   // Pointer to the first row of ‘arr’

8. String Handling:

  • Strings in C are represented as arrays of characters terminated by a null character (‘\0’).
  • Pointers are commonly used for string manipulation and accessing individual characters in s

char str[] = “Hello”;

char *ptr = str;

printf(“%c\n”, *ptr);      // Output: ‘H’ (First character of the string)

printf(“%c\n”, *(ptr+1));  // Output: ‘e’ (Second character of the string)

Understanding the relationship between pointers and arrays is fundamental to efficient memory management, array manipulation, and string handling in C programming. Mastery of these concepts enables you to write more concise, efficient, and maintainable code.