0
0
CComparisonBeginner · 3 min read

Array Name vs Pointer in C: Key Differences and Usage

In C, an array name represents a fixed block of memory and acts like a constant pointer to its first element, while a pointer is a variable that can hold the address of any memory location. Unlike array names, pointers can be reassigned to point elsewhere, but arrays have a fixed size and location determined at compile time.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of array names and pointers in C:

AspectArray NamePointer
Memory LocationFixed at compile timeCan point anywhere at runtime
ReassignmentCannot be reassignedCan be reassigned to different addresses
SizeSize is fixed and knownNo fixed size, points to any type/size
DeclarationDeclared as type name[size]Declared as type *name
UsageUsed to access fixed-size blocksUsed for dynamic memory and flexible access
Pointer ArithmeticAllowed but array name is not a modifiable lvalueAllowed and pointer can move
⚖️

Key Differences

An array name in C is essentially a label for a continuous block of memory allocated for elements of a specific type and size. It acts like a constant pointer to the first element, but you cannot change where it points. This means you cannot assign a new address to an array name after declaration.

On the other hand, a pointer is a variable that stores a memory address and can be reassigned to point to different locations during program execution. Pointers provide flexibility to work with dynamic memory, different arrays, or single variables.

While both array names and pointers support pointer arithmetic (like incrementing to move to the next element), only pointers can be incremented or reassigned as variables. Arrays have a fixed size known at compile time, whereas pointers can point to memory blocks of varying sizes, including dynamically allocated memory.

⚖️

Code Comparison

This example shows how to access array elements using an array name:

c
#include <stdio.h>

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

Pointer Equivalent

Here is the equivalent code using a pointer to access the same elements:

c
#include <stdio.h>

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

When to Use Which

Choose an array name when you need a fixed-size collection of elements known at compile time and do not need to change the memory location. Arrays are simple and efficient for static data.

Choose a pointer when you need flexibility to point to different memory locations, work with dynamic memory allocation, or manipulate data structures like linked lists. Pointers allow more control but require careful management to avoid errors.

Key Takeaways

Array names represent fixed memory blocks and cannot be reassigned.
Pointers are variables that can point anywhere and be reassigned.
Use arrays for fixed-size data and pointers for dynamic or flexible memory access.
Both support pointer arithmetic, but only pointers are modifiable lvalues.
Understanding the difference helps write safer and more efficient C code.