I’m learning C++, and I’m having some trouble in understanding C arrays in depth. I’ve used them in many programs without any problem believing that they just were pointers to their first element with a nice syntax to do pointer arithmetics... but they aren’t, right?
As far as my current understanding goes, arrays are of type someType[someLength]
while pointers are of type someType*
, so they are clearly different. But some confusion for most beginners is made by the fact that arrays passed by value or pointer to functions “decay” into pointers to their first element, which is also the address of the array itself. Is this correct (aside being obviously incomplete)?
Now, my actual question(s), what are exactly arrays?
Why is it useful to have distinct types for array, pointers to first member of array and pointers to arrays? And since that clearly has to be useful for some reason, why can arrays implicitly decay? (or is this decay considered explicit?)
How does an array of n elements of type t differ from a struct whit n members of type t and nothing else? (I know they are different, but I think I only see some of the differences)