#include "stdio.h" #include "stdlib.h" // Returns an array of [n, n+1, ..., m-1, m] // If n > m, then the array returned is []. // If an error occurs, NULL is returned. int *RangeArray(int n, int m) { int length; int *arry; // XXX We must check this explicitly. if (n > m) return (int*)malloc(0); // Heap-allocate the array needed to return. length = m-n+1; arry = (int*)malloc(sizeof(int)*length); // XXX We need to check is malloc'd returned successfully. if (arry == NULL) return NULL; // Initialize the elements. // XXX We had an off-by-one error here. for (int i = 0; i < length; ++i) arry[i] = i+n; return arry; } int main(int argc, char *argv[]) { if (argc != 3) return EXIT_FAILURE; int n = atoi(argv[1]), m = atoi(argv[2]); int *nums = RangeArray(n, m); // XXX We must check the success of RangeArray if (nums == NULL) return EXIT_FAILURE; // Print the resulting array. // XXX We had another off-by-one error here. for (int i = 0; i < (m-n+1); ++i) printf("%d ", nums[i]); puts(""); // XXX We must remember to free everything before quitting. free(nums); return EXIT_SUCCESS; }