What is array decaying in C/C++?

C++Server Side ProgrammingProgramming

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".

raja
Published on 28-Feb-2018 06:50:42
Advertisements