// Example of why file-scoped entities are usually undesirable // substitutes for parameters. // // Note how a user of printList() is limited by the function's // dependence on an external value. // #include #define SZ 10 void initWithSquares(int List[]); void initWithDiffs(int Target[], int Source[]); void printList(FILE* fp, int List[]); int main() { FILE *Out = fopen("listing.txt", "w"); int A[SZ] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; initWithSquares(A); // This is all OK. fprintf(Out, "A:\n"); printList(Out, A); int B[SZ-1]; initWithDiffs(B, A); fprintf(Out, "B:\n"); printList(Out, B); // But printList() has no way to discover // how many values are in B... fclose(Out); return 0; } void initWithSquares(int List[]) { for (int i = 0; i < SZ; i++) { List[i] = i*i; } } void initWithDiffs(int Target[], int Source[]) { for (int i = 0; i < SZ-1; i++) { Target[i] = Source[i+1] - Source[i]; } } void printList(FILE* fp, int List[]) { for (int i = 0; i < SZ; i++) { fprintf(fp, "%5d: %d\n", i, List[i]); } }