Re: G_ARRAY_LENGTH for glib.h?



> > An aside: In the many years that I have used a macro like this, the most
> > common mistake people made was to change code from using an array to using a
> > dynamically allocated array; my ARRAY_LENGTH macro would then evaluate to 1.
> > It would be better if it just didn't compile. In C++, there's a way to do
> > this with a function template; it just won't compile if you pass it a
> > pointer. I'd love to find a way to do this in C with some GCC extension.
> 
> Hmmm, I can't think of any way of doing that. sizeof() is about the only
> operator in C that distinguishes an array from a pointer to the array
> element. (GCC adds typeof(), but I don't think it helps.)


#define G_IS_STATIC_ARRAY(array)  ((void*)&(array) == (void*)&((array[0])))

is almost dangerously clever, but works I think....

- Dave



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