// Simple example illustrating passing an array to a function // Andrew Taylor - andrewt@unsw.edu.au // 17/03/17 #include // passing an array to a function is equivalent to passing a pointer // to its first element so changes to the array in the function // will be visible outside the function // note the array size does not need to be specified // for an array parameter // but the function needs to know the array size // so it is passed as a separate parameter void exponentiate(double x, double powers[], int len) { int i; double power; power = 1; i = 0; while (i < len) { powers[i] = power; power = power * x; i = i + 1; } } #define ARRAY_SIZE 10 int main(void) { int i; double powerArray[ARRAY_SIZE]; exponentiate(42, powerArray, ARRAY_SIZE); i = 0; while (i < ARRAY_SIZE) { printf("42^%d = %lf\n", i, powerArray[i]); i = i + 1; } return 0; }