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: 11-Feb-2020

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements