next up previous contents index
Next: Some hilights of the Up: Declaring and using functions Previous: Calling functions defined anywhere   Contents   Index


Passing arrays, 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);
}
Though 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[]) {
 /* .. */
}
but you will probably need to know the size of the array at runtime, and you are likely to end up with something like

  
int zero_array(double a[], int size_of_array) {  
 /* .. */
}
You can also pass functions as arguments to a function, using function pointers.


next up previous contents index
Next: Some hilights of the Up: Declaring and using functions Previous: Calling functions defined anywhere   Contents   Index
CATAM admin 2010-02-23