Re: null function pointer?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 07 Oct 2005 22:18:14 GMT
"Alexei A. Frounze" <alexfru@xxxxxxx> writes:
> Seems like, to make sure that a pointer doesn't point to an object/function,
> NULL (or simply 0) is good enough for both kind of pointers, data pointers
> and function pointers as per 6.3.2.3:
>
> 3 An integer constant expression with the value 0, or such an expression
> cast to type void *, is called a null pointer constant.55)
> If a null pointer constant is converted to a pointer type, the resulting
> pointer, called a null pointer, is guaranteed to compare unequal to a
> pointer to any object or function.
>
> 4 Conversion of a null pointer to another pointer type yields a null pointer
> of that type.
> Any two null pointers shall compare equal.
>
> It's a stupid question, but how about comparing data to function pointers?
> Null function pointer should compare equal to null data pointer, right,
> wrong? Doesn't different bit representation of the pointers make problems
> here or are they solved through casting or is this not really
> specified/defined?
I think that "Any two null pointers shall compare equal" is intended
to refer only to pointers that *can* be compared. (And I think the
wording is slightly sloppy.)
You can convert a null pointer constant to either a pointer-to-object
type or to a pointer-to-function type (because the standard
specifically says so). There is no conversion, explicit or implicit,
defined between object pointers and function pointers (though some
implementations might allow such conversions as an extension).
So, given
int *obj_ptr = NULL;
void (*func_ptr)(void) = NULL;
the following expression:
obj_ptr == func_ptr
is a constraint violation, because the types are incompatible. Even
casting one argument to the other's type won't help, because no
conversion is defined.
(Arguments that the comparison doesn't make sense don't really answer
the question. C allows plenty of things that don't make sense. The
relevant point here is that this happens not to be one of them.)
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Follow-Ups:
- Re: null function pointer?
- From: Tim Rentsch
- Re: null function pointer?
- From: Alexei A. Frounze
- Re: null function pointer?
- References:
- null function pointer?
- From: Alexei A. Frounze
- null function pointer?
- Prev by Date: Re: Dynamic Array
- Next by Date: Re: malloc trouble
- Previous by thread: Re: null function pointer?
- Next by thread: Re: null function pointer?
- Index(es):
Relevant Pages
|