arrays/array-limitations-3a.c - Illustration af at arrays ikke umiddelbart kan returneres fra en funktion - compiler warnings. | Lektion 9 - slide 4 : 30 Program 5 |
#include <stdio.h> #define TABLE_SIZE 11 double* f (int n){ double a[TABLE_SIZE]; // A local array. 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. // Compiler Warning: // Returns address of a local variable. } int main(void) { int i; double *c; c = f(3); // c now refer to a de-allocated array // returned from f. // Printing elements of returned array. // Hoping that all elements are 3: for(i = 0; i < TABLE_SIZE; i++) printf("After calling f: c[%d] = %f\n", i, c[i]); return 0; }