๐Ÿ“ C Pointers
Estimated reading: 4 minutes 6 views

๐Ÿงฑ C Pointer to Array โ€“ Accessing Entire Arrays via Pointers in C


๐Ÿงฒ Introduction โ€“ What Is a Pointer to an Array in C?

In C programming, a pointer to an array is a pointer that points to the entire arrayโ€”not just its first element. Unlike a regular pointer (e.g., int *p), which points to a single element, a pointer to an array (e.g., int (*p)[5]) provides access to the whole array block as a unit.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • The syntax of a pointer to an array
  • How it’s different from a pointer to the first element
  • Examples using both 1D and 2D arrays
  • Best practices and practical use cases

๐Ÿ” Core Concept โ€“ Pointer to an Entire Array

When you use int (*ptr)[5];, you’re declaring ptr as a pointer to an array of 5 integers.

โœ… Syntax:

int arr[5];
int (*ptr)[5] = &arr;
  • ptr holds the address of the whole array block
  • *ptr dereferences to the array
  • (*ptr)[i] accesses individual elements

๐Ÿ’ป Code Examples โ€“ Pointer to Array in Action

โœ… Example 1: Accessing Elements via Array Pointer

#include <stdio.h>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int (*ptr)[5] = &arr;

    for (int i = 0; i < 5; i++) {
        printf("%d ", (*ptr)[i]);
    }

    return 0;
}

๐Ÿ–จ๏ธ Output:

10 20 30 40 50

๐Ÿ“˜ (*ptr)[i] accesses elements from the array that ptr points to.


โœ… Example 2: Pointer to 2D Array

#include <stdio.h>

int main() {
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int (*p)[3] = matrix;  // Points to row arrays of size 3

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", p[i][j]);
        }
        printf("\n");
    }

    return 0;
}

๐Ÿ–จ๏ธ Output:

1 2 3  
4 5 6

๐Ÿ“Œ p treats matrix as an array of arrays (rows), each of 3 integers.


๐Ÿงฉ Pointer to Array vs Array Name vs Pointer to Element

ExpressionMeaning
int *p = arrPointer to first element (arr[0])
int (*p)[5]Pointer to the entire array of 5 int
arrDecays to address of arr[0]
&arrAddress of the whole array

๐Ÿ’ก Best Practices & Tips

๐Ÿ“˜ Best Practice:

  • Use pointer-to-array when you want to pass an entire array to a function with size context.

๐Ÿ’ก Tip:

  • Prefer int (*p)[N] over int *p for multi-dimensional arrays or when element grouping matters.

โš ๏ธ Pitfall:

  • *p[i] is not the same as (*p)[i]. Always use parentheses to correctly dereference array pointers.

๐Ÿ› ๏ธ Real-World Applications

  • ๐Ÿ“ Multi-dimensional matrix processing
  • ๐Ÿ“ฆ Passing large arrays to functions safely
  • ๐ŸŽฎ Game engines (grid maps, tile rendering)
  • ๐Ÿง  Memory-efficient structure layouts in embedded systems

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Pointers to arrays allow working with entire blocks of memory representing arrays. This enhances control over how data is accessed and manipulated, especially in multi-dimensional scenarios.

๐Ÿ” Key Takeaways:

  • int (*ptr)[N] is a pointer to an array of N integers
  • Use (*ptr)[i] to access array elements
  • Different from int *p which only points to the first element
  • Vital in 2D array handling and advanced memory management

โš™๏ธ Real-World Relevance:

Used in matrix computation, 2D graphics, firmware memory layouts, and high-performance code.


โ“ Frequently Asked Questions (FAQ)

โ“ What is the difference between int *p = arr and int (*p)[5] = &arr?

โœ… int *p points to the first element, while int (*p)[5] points to the entire array as a unit.


โ“ How do I pass an entire array to a function using a pointer?

โœ… Use int (*p)[N] as a parameter and pass the address: func(&arr);


โ“ Can I use pointer to array with malloc()?

โœ… Yes, especially useful for dynamically allocated 2D arrays:

int (*matrix)[cols] = malloc(rows * sizeof(*matrix));

โ“ Is arr == &arr[0] the same as &arr?

โŒ No.

  • arr and &arr[0] both point to the first element.
  • &arr points to the entire array block.

โ“ When should I use a pointer to an array?

โœ… Use when working with fixed-size blocks, 2D arrays, or when needing size context and tight memory layout.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿงฑ C Pointer to Array

Or Copy Link

CONTENTS
Scroll to Top