Is an Array Name a Pointer?
Last Updated :
18 Jun, 2024
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;
}
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;
}
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;
}
OutputSize 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++.
Similar Reads
Pointer to an Array in C++
Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or
6 min read
Pointer to an Array | Array Pointer
A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements. Consider the following example: [GFGTABS] C #include<stdio.h> int main() { int arr[5] =
5 min read
Pointer vs Array in C
Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being:  1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2.
1 min read
Pointers vs Array in C++
Arrays and pointers are two derived data types in C++ that have a lot in common. In some cases, we can even use pointers in place of arrays. But even though they are so closely related, they are still different entities. In this article, we will study how the arrays and pointers are different from e
3 min read
Array of Pointers in C
In C, a pointer array is a homogeneous collection of indexed pointer variables that are references to a memory location. It is generally used in C Programming when we want to point at multiple memory locations of a similar data type in our C program. We can access the data by dereferencing the point
6 min read
Difference between Arrays and Pointers
The array and pointers are derived data types that have lots of differences and similarities. In some cases, we can even use pointers in place of an array, and arrays automatically get converted to pointers when passed to a function. So, it is necessary to know about the differences between arrays a
7 min read
Maximum Size of an Array in C
Array in C is a collection of elements of the same data type that are stored in contiguous memory locations. The size of an array is determined by the number of elements it can store and there is a limit on this size. The maximum size of an array in C is determined by many factors, including the dat
4 min read
Array of Pointers in Objective-C
A pointer is a variable that stores the address of another variable. We use pointers because, with the help of pointers the memory is accessed efficiently, it saves memory space, and execution time is faster as compared to the use of normal variables using stack memory because pointers store their m
5 min read
Pointer to Arrays in Objective-C
In Objective-C, a pointer to an array is a way to store multiple values of the same data type in contiguous memory locations. These arrays can be manipulated and accessed using pointers, which are variables that store the memory address of another variable. In Objective-C, pointers to arrays are use
4 min read
Dynamic Array in C
Array in C is static in nature, so its size should be known at compile time and we can't change the size of the array after its declaration. Due to this, we may encounter situations where our array doesn't have enough space left for required elements or we allotted more than the required memory lead
9 min read