| arrays/array-limitations-4a.c - Illustration af at arrays kan returneres fra en funktion - med et lille trick - compilerer og kører korrekt. | Lektion 9 - slide 4 : 30 Program 7 |
#include <stdio.h>
#define TABLE_SIZE 11
double* f (int n){
static double a[TABLE_SIZE]; // A local array, storage class static.
int i;
// Initializing all elements of a to n:
for(i = 0; i < TABLE_SIZE; i++)
a[i] = (double)n;
return a; // Returning local array.
// No compiler warning any more.
}
int main(void) {
int i;
double *c;
c = f(3); // c now refer to array returned from f
for(i = 0; i < TABLE_SIZE; i++)
printf("After calling f: c[%d] = %f\n", i, c[i]);
return 0;
}