Today encountered a one-dimensional array to do function parameters, then grilled the problem:
First, the conclusion:
1: In C, when a one-dimensional array is a function parameter, the compiler always parses it into a pointer to its first element.
2: The actual passed array size has no relation to the size of the array specified by the function parameter.
Then illustrate:
The following is an element exchange function that swaps the array "I" and the "J" of the arrays. Notice how the array is passed to the function.
Correct notation 1:
Explanation: The compiler parses an array into pointers to the shaping elements, that is, the first address of the array, with no specified number in the square brackets, because the compiler does not look at all, so it is best not to write, so as not to cause misunderstanding.
Correct wording 2:
Explanation: This is more straightforward to write, the parameter is a pointer to the shaping element, and the name of the array is the address of the first element, also confirms that the array name is a pointer constant.
There may be some classmates on the function of Array[i], Array[j] do not understand, in fact, array[i] = * (array+i), explaining that the first address plus the offset address I, is pointing to the first element.
Array (i) = (array +i) // This is a pointer to the first element.
Array[i] = * (array+i) // This is the first element of the array.
Notice () and [], don't look wrong.
Finish.
A C-language one-dimensional array is passed to the function as a parameter: