B
BartC
Eric Sosman said:Eric Sosman said:On 12/11/2010 5:53 PM, BartC wrote:[...] an array has 0 elements, therefore
it's
pointer is 0 -- where would it point to otherwise?
You have an array of zero elements? In a C program? How did
you manage that? Was this before or after you squared the circle?
It was just after I wrote this:
[...]
int *array=0;
[...]
The array is zero length at the time of the first print_intarray() call.
"The array?" What array? Perhaps you should read Section 6
of the FAQ. (If you've already read it, re-read it. This time,
read for comprehension.)
None of the section headings leap out at me. Perhaps you mean the bit about
arrays and pointers being identical, so that an array needs to be a valid
pointer to something, and NULL is not allowed.
Well, OK. In that case it's necessary to use a little bit of abstraction and
use a slightly higher level of array concept. For example, the 'intarray' I
created in that code example.
For my intarrays, I *can* have an array of length zero. The pointer value is
not critical, since it should not need to be dereferenced (unlike an empty
string which needs to get at the zero terminator). It can be NULL as I've
used, or perhaps point into valid memory to create a slice of 0 to N
elements, depending on intarray length. Or whatever.
As shown, I can use a function such print_intarray to display an array of 0
to N elements.
I could also write one called append_intarray to add an element to an
intarray, increasing it's length from 0 to N elements, to 1 to N' elements.
So such an 'array' can be perfectly well-behaved even when it's empty, even
though it may not exactly correspond to C's official definition of an array,
whatever that is. (And there's a guy over on comp.lang.misc who insists that
C doesn't have arrays at all..)