So, I've just learnt about arrays, pointers and decaying but I don't really understand.
If "arrays" (the things declared with int arr[]
) only decay to pointers when passed to functions, why does an array equal a pointer to its first element when evaluated in main? Furthermore, if "arrays" in main are not pointers, but arr[x]
is syntactic sugar for *(arr + x)
, why is the dereferencing required if the "array" is not a pointer. Adding to my confusion is the fact that the sizeof(arr) in main returns the correct value, while in a function it returns the size of the pointer. Furthermore, the CS course I am taking stated that the name of an "array" is actually a pointer to its first element, however again, this is not what I have heard from other sources.
Also, if "arrays" are actually pointers, is there any way to directly create an array, skipping the intermediate pointer (if you would even want to do such a thing).
Can anyone help clear all this up for me?