What is Array Decaying in C/C++



Arrays and pointers work quite similarly in C/C++. But there are some subtle differences. For example, the sizeof operator works quite differently on the two. When you convert an array in a pointer,

Example

#include<iostream>

int main() {
   const int a[] = { 2, 3, 5, 7, 11 };
   const int* p = a;
   std::cout << ( sizeof(p) != sizeof(a) );
}

Output

This gives the output −

1

The sizeof operator on the pointer actually gives the size of the pointer rather than that of the array. This loss of ability of a pointer is called "decay".

Updated on: 2020-02-11T10:41:41+05:30

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements