Open In App

Is an Array Name a Pointer?

Last Updated : 18 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, arrays and pointers are closely related and are often considered same by many people but this a common misconception. Array names are not a pointer. It is actually a label that refers to a contiguous block of memory where the elements are stored.

In this article, we will learn more about the array name and how it is similar and different from the pointers in C.

Similarities between the Array Name and Pointers

Below are the similarities between array name and pointers in C:

1. Address Representation

An array name can be used to represent the address of the first element of the array. For example, if arr is an array, arr can be used to refer to the address of arr[0].

Example

C
#include <stdio.h>

int main()
{
    // array
    int arr[5] = { 1, 2, 2, 1, 5 };
    // pointer to arr name
    int* ptr = arr;

    printf("*ptr = %d\n*arr = %d", *ptr, *arr);

    return 0;
}

Output
*ptr = 1
*arr = 1

2. Array Subscript Notation

Both array names and pointers support array subscript notation for accessing elements.

C
#include <stdio.h>

int main()
{

    int arr[5] = { 1, 2, 3, 4, 5 };
    int* ptr = arr;
    // Accesses the third element of the array
    int value = ptr[2];

    printf("ptr[2] = %d", value);

    return 0;
}

Output
ptr[2] = 3

3. Pointer Arithmetic Compatibility

Pointer arithmetic can be applied in contexts involving both array names and pointers. But keep in mind that we cannot change the value of the array name. It is constant and will always refer to the same array.

Example

C
#include <stdio.h>

int main()
{

    int arr[5] = { 1, 2, 3, 4, 5 };
    int* ptr = arr;
    int value1 = *(arr + 2); // Equivalent to arr[2]
    int value2 = *(ptr + 2); // Equivalent to ptr[2]

    printf("*(arr + 2): %d\n*(ptr + 2): %d", value1,
           value2);

    return 0;
}

Output
*(arr + 2): 3
*(ptr + 2): 3

Difference Between the Array Name and Pointer

Below are the differences between array name and pointers in C:

1. Nature and Definition

An array name represents a fixed-size collection of elements and refers to the memory location of the first element of the array. While a pointer is a variable that holds the memory address of another variable.

2. Memory and Size

The memory size of an array is the total size of all its elements while the size of a pointer is fixed, typically 4 bytes on a 32-bit system and 8 bytes on a 64-bit system, regardless of the data it points to.

Example

C
#include <stdio.h>

int main()
{

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

    // printing size
    printf("Size of arr: %d\n", sizeof(arr));
    printf("Size of ptr: %d", sizeof(ptr));

    return 0;
}

Output
Size of arr: 20
Size of ptr: 8

3. Reassignment

An array name is immutable in terms of reassignment. Once an array is declared, its name always points to the same memory location.

A pointer can be reassigned to point to different memory locations during its lifetime as long as it is not of const type.

Example

The following program will throw an error.

C
#include <stdio.h>

int main()
{

    int arr[5] = { 1, 2, 3, 4, 5 };
    // this will throw error
    arr++;

    printf("*arr++: %d", *arr);

    return 0;
}


Output

main.c: In function ‘main’:
main.c:8:8: error: lvalue required as increment operand
8 | arr++;
| ^~

Conclusion

While an array name is not a pointer, it often behaves like one due to array-to-pointer decay. Understanding the differences and similarities between array names and pointers is crucial for effective programming in C and C++.



Next Article

Similar Reads