C Passing Arrays to Functions
If you want to pass a one-dimensional array as an argument in a function, you must declare the function formal parameter in one of the following three ways. The results of these three declaration methods are the same, because each method tells the compiler that an integer pointer will be received. Similarly, you can also pass a multi-dimensional array as a formal parameter.
Method 1
A formal parameter is a pointer (you can learn about pointers in the next chapter):
Method 2
A formal parameter is an array with a defined size:
Method 3
A formal parameter is an array with an undefined size:
Example
Now, let's look at the following function, which takes an array as a parameter and also passes another parameter. Based on the parameter passed, it returns the average of the elements in the array:
Now, let's call the above function as follows:
Example
When the above code is compiled and executed, it produces the following results:
平均值是: 214.400000
As you can see, as far as the function is concerned, the length of the array is irrelevant, because C does not perform bounds checking on formal parameters.
other extensions