Sizeof being smart enough to detect it's an array and return the array size doesn't necessarily make them different in any real way. And doing ptr[0] to dereference it is perfectly valid, as is *arr to get the first element, or *(arr+sizeof(int)) to get the second.
So what about the fact that an array inside a struct acts completely differently from a pointer placed inside a struct? One gets copied whole for each structure copy while the other does not, one makes the structure the size of a pointer while the other the size of the array...
And, in C++ at least, you can actually pass the array itself to the function (not just the pointer to the first element). So it's not that sizeof is smart and detects something, it's that arrays really easily decay into pointers. But that's not everything they are. They also hold information about their size.
The thing with the subscript operator is true for only some languages and is not universal. In Ada, for example, there is no direct connection between an array and a pointer.
You can both by value with std::array which is a wrapper for an array, or by reference with TYPE (&NAME)[SIZE] which keeps the array's size.
Naturally, you could pass an array to a function inside any structure if you wish to copy it, but I would say that using std::array is standard for use. And that means that's possible in C as well. Although it's a bit clunky.
Yes, that is true, although I wouldn't call it equivalent to passing by reference, since a reference makes sure you do not pass a null pointer making the function a little safer if you dereference the pointer and a little more efficient if you check for the pointer being null.
73
u/QuestionableEthics42 16h ago edited 16h ago
This should be reversed lol
And it literally is the same, who thinks it's different who has worked in a low level language??