what is array decay ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

what is array decay ?

I don't want just the definition but a real simplified answer with example. I tried to find answer on the internet but couldn't understand. ( I am new to cpp)

1st Oct 2018, 10:01 AM
Mr. Bot
Mr. Bot - avatar
6 Answers
+ 19
Arrays are basically the same as pointers in C/C++, but not quite. Once you convert an array: const int a[] = { 2, 3, 5, 7, 11 }; into a pointer (which works without casting, and therefore can happen unexpectedly in some cases): const int* p = a; you lose the ability of the sizeofoperator to count elements in the array: assert( sizeof(p) != sizeof(a) ); // sizes are not equal This lost ability is referred to as "decay".
1st Oct 2018, 10:52 AM
👑Mahesh Khatri👑
👑Mahesh Khatri👑 - avatar
1st Oct 2018, 8:05 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
Thankx Mahesh and Anjali. Now I got the idea of array decay and how it is threat for array of characters, it's basically threat to any array because after converting an array to a pointer we will not able to access it's size. The array will decay to a single pointer. And without knowing it's size we don't know the end of the array which means we lost the actual array. PLEASE CORRECT ME IF I AM WRONG
1st Oct 2018, 5:25 PM
Mr. Bot
Mr. Bot - avatar
+ 2
i don't know, but i would guess something to do with time constraint, maybe purposely set to self destruct at a set rate of dcay.. just wild guess
1st Oct 2018, 10:19 AM
niteOwLTwO
niteOwLTwO - avatar
+ 2
How ?
1st Oct 2018, 1:35 PM
Mr. Bot
Mr. Bot - avatar
+ 1
Mahesh I was reading an article about "array of char vs string". One of the difference given their between these two was array of char has threat of array decay. I can't relate your answer to this . please explain. :(
1st Oct 2018, 11:32 AM
Mr. Bot
Mr. Bot - avatar