[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] C programming, variable size array



On Fri, Dec 12, 2003 at 08:38:18PM +0200, Aryan Ameri wrote:
> 
> "Write a program, which promts the uset to enter some numbers. The user 
> should terminate the sequence of numbers by entering EOF character. The 
> program should put numbers entered by the user in to a 1D array".

The real answer is linked lists.  But since you're forced to use arrays, 
use malloc to create the array to some initial size, realloc to make the 
array bigger later if necessary, and free it when you're done.

You can increase the array by say 100 elements, but in "real code" they 
increase to the nearest power of 2, so you don't get memory 
fragmentation.



Reply to: