0
0
CComparisonBeginner · 4 min read

Pointer vs Array in C: Key Differences and Usage

In C, arrays are fixed-size blocks of memory with a name representing their address, while pointers are variables that store memory addresses and can be reassigned. Arrays cannot be resized or assigned directly, but pointers offer more flexibility in memory manipulation.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of pointers and arrays in C.

AspectArrayPointer
DefinitionFixed-size block of memory with a nameVariable storing a memory address
SizeFixed at compile timeCan point to any size or location
MutabilityName is constant, elements can changePointer variable can be reassigned
Memory LocationContinuous memory allocatedCan point anywhere in memory
AssignmentCannot assign whole array directlyCan assign pointer to different addresses
UsageUsed for fixed collectionsUsed for dynamic memory and flexible access
⚖️

Key Differences

Arrays in C represent a fixed block of memory allocated for elements of the same type. The array name acts like a constant pointer to the first element, but you cannot change this address. You can access elements using indexing, and the size is fixed at compile time.

Pointers are variables that store memory addresses and can be reassigned to point anywhere. They allow more flexible memory management, such as dynamic allocation or iterating through memory. Unlike arrays, pointers can be incremented or decremented to traverse memory locations.

While arrays and pointers often seem similar because array names decay to pointers in expressions, arrays allocate memory automatically, whereas pointers require explicit memory management. Also, you cannot assign one array to another directly, but you can assign pointers freely.

⚖️

Code Comparison

This example shows how to print elements of an integer collection using an array.

c
#include <stdio.h>

int main() {
    int arr[3] = {10, 20, 30};
    for (int i = 0; i < 3; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
Output
10 20 30
↔️

Pointer Equivalent

The same task done with a pointer pointing to the first element of an array.

c
#include <stdio.h>

int main() {
    int arr[3] = {10, 20, 30};
    int *ptr = arr; // pointer to first element
    for (int i = 0; i < 3; i++) {
        printf("%d ", *(ptr + i));
    }
    return 0;
}
Output
10 20 30
🎯

When to Use Which

Choose arrays when you know the fixed size of data at compile time and want simple, safe access to elements. Arrays are easier to read and less error-prone for fixed collections.

Choose pointers when you need flexibility, such as dynamic memory allocation, passing large data efficiently, or manipulating memory addresses directly. Pointers are essential for advanced memory management and dynamic data structures.

Key Takeaways

Arrays have fixed size and name acts like a constant pointer to their memory.
Pointers store memory addresses and can be reassigned to point anywhere.
Arrays allocate memory automatically; pointers require manual management.
Use arrays for fixed-size collections and pointers for dynamic or flexible memory.
Pointer arithmetic allows traversing memory, unlike array names.