From Code 1, whenever arrays are passed as the arguments to functions, they are always passed by using the 'Pass by reference' mechanism. Because of this, they will decay into pointers in the function parameters.
The loss of type and dimensions of an array is known as array decay. It occurs when we pass the array into a function by pointer or value. First address is sent to the array which is a pointer. That is why, the size of array is not the original one.
You probably heard that "arrays are pointers", but, this is not exactly true (the sizeof inside main prints the correct size). However, when passed, the array decays to pointer. That is, regardless of what the syntax shows, you actually pass a pointer, and the function actually receives a pointer.
Not only can a pointer store the address of a single variable, it can also store the address of cells of an array. Consider this example: int *ptr; int arr[5]; // store the address of the first // element of arr in ptr ptr = arr; Here, ptr is a pointer variable while arr is an int array.
Sure.
In C99 there are three fundamental cases, namely:
when it's the argument of the &
(address-of) operator.
when it's the argument of the sizeof
operator.
When it's a string literal of type char [N + 1]
or a wide string literal of type wchar_t [N + 1]
(N
is the length of the string) which is used to initialize an array, as in char str[] = "foo";
or wchar_t wstr[] = L"foo";
.
Furthermore, in C11, the newly introduced alignof
operator doesn't let its array argument decay into a pointer either.
In C++, there are additional rules, for example, when it's passed by reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With