--- In [email protected], "py2akv" <g...@...> wrote:
>
> 1. I'm no pointer specialist, but I suspect, first and
> foremost, null pointer is a misnomer, because a pointer
> always points to something.

No, they don't. They may point to something, or they may be
a null pointer.

> Let's remember a pointer is, per se, also an object,
> only carrying the address of another object.

A pointer is an object yes, but it may point to a function,
which is not an object. It may also point to an incomplete
object type. Observe...

  extern struct x x;
  extern struct x y;
  
  struct x *foo(struct x *p)
  {
    if (p == &x)
      return &x;
    else
      return &y;
  }

There is no declaration of struct x, yet the code is valid
[assuming the objects x and y are defined appropriately
elsewhere.]

> Pointing to an address is one thing, but pointing to an
> object is another thing.

Objects have addresses and so do functions. Don't bring
direct memory mapping or pointer representations into the
mix unless you _have_ to. [And for a beginner's question,
you don't have to.]

You seem to think of a pointer along the lines of a cup.
A cup is never empty because even if there's no tea or
coffee in it, there's still air! This is a needless
complication.

The bit representation of null pointers is not relevant to
understanding them. Indeed, it only adds confusion. You
only need to understand their properties. Once you understand
them, you'll wonder why anyone ever bothered bringing
representations into the mix!

> It's up to the compiler, I think, to choose where a null
> pointer should point to.

Null pointers don't point to anything! That's why they're called
null pointers.

> A null pointer is something pointing to no object?

Yes! A null pointer will never compare equal to an address
of an object or function.

> 2. Wouldn't a void pointer point to a known address in
> memory that you don't know (or doesn't matter) what lies
> there?

No. The representation of a null pointer may not even be
a valid address in the address space of the machine! Note
also that null pointer representations needn't be unique.

There may be hundreds or even millions of null pointer
representations. What matters is that they will all compare
equal to each other, and different to the address of any
object or function.

> Mind, malloc() and family points to an unspecified, but
> known, block of memory and that's why it gives you a
> void pointer.

Void pointers have lots of 'generic' properties. That is
why malloc was standardised to return one in 1989.

-- 
Peter

Reply via email to