/* * C and pointer Chapter 2 the second question reads a series of integers from the standard input, stores these values in a dynamically allocated array, and returns this array. The first element of the array is the length of the array. Other elements are input integers. */ # Include <Stdio. h> # Include <Stdlib. h> // Define a length, which is allocated at the beginning # Define Length 20 Int * Read_ints (){ Int * Num; Int TEM; Int Count = 0 ; Int Size = Length; // Allocate memory Num = malloc (size + 1 )* Sizeof ( Int )); // Memory Allocation failed, return null If (Num =Null) Return NULL; While (Scanf ( " % D " , & TEM )! = EOF) {count ++ ; // If the length is greater than the allocated memory, you need to increase the memory and use realloc for allocation. If (Count> Size) {size + = Length; num = Realloc (Num, (size + 1 )* Sizeof ( Int )); If (Num = Null) Return NULL;} num [count] = TEM ;} If (Count < Size) {num = Realloc (Num, (count + 1 )* Sizeof (Int )); If (Num = Null) Return NULL;} num [ 0 ] = Count; Return Num ;} Int Main (){ Int * Array; Int I; Array = Read_ints (); For (I = 1 ; I <= array [ 0 ]; I ++ ) Printf ( " % D " , Array [I]); Return 0 ;}