structs and functions as arguments to a function
You can pass an array or struct as an argument to a function in the
same way as any other type:
|
int zero_array(double a[10]) {
int i;
for (i=0; i<10; i=i+1) {
a[i] = 0.0;
}
}
int main(void) {
double a[10];
zero_array(a);
}
|
structs are passed by value (see
above for an explanation of `by value'), C's
handling of arrays (see here) means that arrays are effectively passed `by
reference'.
If you do not know the size of the array at compile-time, you can declare the function as:
|
int zero_array(double a[]) {
/* .. */
}
|
|
int zero_array(double a[], int size_of_array) {
/* .. */
}
|