Please help me understand what happens here:
#include <iostream>
void foo(int *ar) {std::cout << sizeof(arr) << '\n' << arr[3];} // exceeding array bounds on purpose
int main()
{
int arr[2]{3, 5};
foo(arr);
return 0;
}
Is this an example of array decay? And what exactly is *arr? Is it a pointer-to-int, or is it a pointer-to-array (of int)? In case of the latter, does it carry information about the address of a memory block holding the array? If not, how is it possible to use array notation inside a function that only recieves a pointer to its first element?
Then,
#include <iostream>
void foo(int *ar) {std::cout << sizeof(arr) << '\n' << arr[1];}
int main()
{
int arr[2][1]{{3}, {5}};
foo(*arr);
return 0;
}
What happens in case of a 2D array? What does *arr represent here when passed? Is it the pointer to the first element of the right array? What does it represent when taken as an argument by a function?
edit: changed array name in foo