+ 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)
7 odpowiedzi
+ 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".
+ 9
Here a useful link on array decay:
https://www.geeksforgeeks.org/what-is-array-decay-in-c-how-can-it-be-prevented/amp/
+ 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
+ 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
+ 2
How ?
+ 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. :(