On Sun, 27 Dec 1998, James [on his mailserver] wrote:
> cos i'd like to...
>
> would this work:
Not at all.
Don't use zero length arrays. K&R C and GNU extensions
give extra rope to hang with, so you shouldn't use them
if possible.
> int thing[0], *ptr = thing;
> int in;
> realloc (ptr, in);
> ....
That which is not malloc'd can not be realloc()'d. ptr is
a pointer to a zero length automatic variable, you can
only *ATTEMPT* to resize dymanically allocated memory with
realloc().
char buffer[6];
int *array;
int items;
while(1)
{
int counter;
printf("Enter a number greater than zero");
fflush(stdout);
if(buffer != fgets(buffer, sizeof(buffer), stdin))
continue;
/* put phrasing and bounds checking here 'continue'
if not ok, items = the valid result */
if(NULL==(array=malloc(sizeof(int) * items)) )
{
perror("malloc()");
}
break;
}