<<< Date Index >>>     <<< Thread Index >>>

Re: Little code cleanup



Vincent Lefevre <vincent <at> vinc17.org> writes:

> 
> On 2008-03-05 14:08:51 +0100, Sébastien Hinderer wrote:
> > In the context of an off-list discussion, a friend of mine wrote what
> > follows (Actually this was written in french and I am translating):
> > 
> > In my opinion, the problem comes from the fact that C does not make a
> > clear distinction between arrays and pointers which are, however, two
> > objects of very different natures. Here fname and mbox are arrays, not
> > pointers.
> 
> They are arrays, but in expressions, they evaluate to pointers
> (with a few exceptions). More precisely:
> [...]
> 
> > This is precisely why one can write things like "sizeof(mbox)".
> > Since the abovementionned variables are arrays, it makes not really
> > sense to verify that their addresses are non-NULL.
> 
> According to the C standard (see above), it makes really sense,
> though the value of the resulting pointer (i.e. after the conversion)
> is always non-null.
> 

Hi all, I'm the friend Sébastien was referring to.

You are missing my point. They EVALUATE to pointers, but they are not pointers.
By "it doesn't make sense", it was not implied that the code was wrong, only
that it is unneeded and resulting of a confusion between an array and a pointer.
And the warning of gcc is meaning the same thing.

If arrays evaluate to pointers it is more because in C you don't have operations
on arrays (unlike in Fortran, for example), so it is convenient to transform
"array" in "&array" in expressions, as it allows to have the same syntax as if
they where pointers. But from this very fact comes the confusion!

Would you write a function like that:
int foo(void)
{
        int bar;
        if (&bar)
                bar = 3;
        return bar;
}

It is perfectly valid C, as are the lines which generate the warnings in mutt's
code, but I don't think you would write it: The situation is the same, but for
the fact that in the expression in the 'if', "mbox" is automatically converted
in "&mbox". However, as "mbox", here, is an array and not a real pointer the
compiler knows at compile time the address it will have, hence the warning.

A warning doesn't necessarily means the code is wrong (for these cases, you have
something else, called an "error" ;-) ), just that it is strange!

Regards,
N.