Re: null function pointer?



"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.
.



Relevant Pages

  • Re: multi dimensional arrays as one dimension array
    ... Even if that is the case, I still don't see where the conversion for other ... const int *cpz = z; ... "For any qualifier q, a pointer to ... Two pointers can compare equal and yet still ...
    (comp.lang.c)
  • Re: [PATCH] x86, ioremap: use %pR in printk
    ... return string(buf, end, sym, field_width, precision, flags); ... %pF output the name of a function pointer ... %pR output the address range in a struct resource ... * function pointers are really function descriptors, ...
    (Linux-Kernel)
  • Re: [PATCH] x86, ioremap: use %pR in printk
    ... return string(buf, end, sym, field_width, precision, flags); ... %pF output the name of a function pointer ... %pR output the address range in a struct resource ... * function pointers are really function descriptors, ...
    (Linux-Kernel)
  • Re: [PATCH] x86, ioremap: use %pR in printk
    ... return symbol_string(buf, end, ptr, field_width, precision, flags); ... %pF output the name of a function pointer ... %pR output the address range in a struct resource ... * function pointers are really function descriptors, ...
    (Linux-Kernel)
  • Re: Is the following code OK?
    ... >> I have to compare an address of structure with an absolute address(this ... >> pointer and an unsigned int are being compared. ... the result of the conversion is implementation- ...
    (comp.lang.c)