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

C Pointers and Arrays โ€“ Understanding Their Relationship in C


Introduction โ€“ How Are Pointers and Arrays Related in C?

In C programming, pointers and arrays are closely linked. In many contexts, the name of an array acts as a pointer to its first element. Understanding how they interact allows developers to use memory more efficiently and write cleaner, faster code.

In this guide, youโ€™ll learn:

  • How arrays and pointers relate in memory
  • How to use pointers to access and manipulate array elements
  • Differences between arrays and pointers
  • Best practices and common mistakes

Core Concept โ€“ Arrays as Pointers

When you declare an array, its name represents the address of the first element.

int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;       // Equivalent to int *p = &arr[0];
  • arr is essentially a constant pointer: arr == &arr[0]
  • You can access elements with either arr[i] or *(arr + i)

Code Examples โ€“ Pointer & Array Access

Example 1: Accessing Array with Pointer

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int *p = arr;

    for (int i = 0; i < 5; i++) {
        printf("%d ", *(p + i));  // Equivalent to arr[i]
    }

    return 0;
}

Output:

1 2 3 4 5

Example 2: Modifying Array Elements Using Pointers

#include <stdio.h>

int main() {
    int arr[3] = {5, 10, 15};
    int *ptr = arr;

    *(ptr + 1) = 20;  // Change second element

    printf("arr[1] = %d\n", arr[1]);  // Output: 20

    return 0;
}

This shows that changing *(ptr + 1) also changes arr[1].


Example 3: Array Not Equal to Pointer in All Contexts

#include <stdio.h>

int main() {
    int arr[5];
    printf("Size of arr = %lu\n", sizeof(arr));   // Usually 20
    int *p = arr;
    printf("Size of p = %lu\n", sizeof(p));       // Usually 8 (on 64-bit)

    return 0;
}

sizeof(arr) returns the size of the entire array, while sizeof(p) returns the size of the pointer.


Key Differences โ€“ Array vs Pointer

FeatureArrayPointer
Memory AllocationAllocated at compile timeCan be allocated dynamically
Mutabilityarr is a fixed base addressptr can be reassigned
Size (using sizeof)Gives total size of arrayGives size of the pointer only
TypeFixed-size collectionVariable that stores an address

Best Practices & Tips

Best Practice: Use array syntax (arr[i]) for clarity unless pointer arithmetic is required.

Tip: When passing arrays to functions, remember they decay to pointers โ€” no size information is passed.

Pitfall: Do not modify array names (arr = new_arr; is illegal), but pointers can be reassigned freely.


Use Cases & Applications

  • Efficient iteration and manipulation of array elements
  • ๐Ÿ“ฉ Passing arrays to functions as pointers
  • Working with dynamic arrays (via malloc)
  • Building strings and string manipulation routines

Summary โ€“ Recap & Next Steps

Understanding the relationship between arrays and pointers allows C programmers to access and manipulate memory efficiently, pass arrays to functions, and build advanced data structures.

Key Takeaways:

  • Array name is a pointer to its first element
  • arr[i] is equivalent to *(arr + i)
  • Arrays and pointers are not the sameโ€”but closely related
  • Arrays are fixed, pointers are flexible

Real-World Relevance:

Critical in string handling, matrix operations, dynamic memory, and buffer manipulation in embedded, systems, and performance-critical applications.


Frequently Asked Questions (FAQ)

Is arr == &arr[0] always true?

Yes, in pointer contexts, the array name arr is treated as the address of its first element.


Can I increment an array like a pointer?

No. Array names are constant and cannot be incremented: arr++ is illegal, but ptr++ is valid.


What happens when I pass an array to a function?

It decays into a pointer to its first element. Size info is lost unless passed separately.


Is *(arr + i) faster than arr[i]?

No difference in performance. They compile to the same code. arr[i] is just syntactic sugar.


Can I use pointers to iterate through multi-dimensional arrays?

Yes, but you must understand memory layout (row-major order). Pointer arithmetic becomes more complex.


Share Now :
Share

๐Ÿ”— C Pointers and Arrays

Or Copy Link

CONTENTS
Scroll to Top