Re: question: Null pointer

> So, you mean to say that if try to access the memory location pointed to
> by a NULL pointer on a system valid for  C implementation i MUST get
> some error ( may be segmentation fault ).

No, if you try to access the memory pointed to by the null pointer you
invoke undefined behaviour. This meens that from that point onwards the
program is free to do anything at all, this incudes producing an error,
formating your harddrive, or even worse seeming to work fine.

> Is this possible, that a some system may define some other location (
> instead of location 0 ) as INVALID and then use that location for a NULL
> pointer.

In C source code setting a pointer to 0, always sets the pointer to the
null pointer, but in runable program the actual representation of the null
pointer is not necessarly zero.

> I have read some articles on C which say that one should never test for
> a pointer to be NULL by testing it with integer 0. If you say that NULL
> pointer always points to location 0, then why can't we test whetehr a
> pointer is null pointer or not by comparing it with integer 0.

Partly it is a style thing. If you compare a pointer with the NULL macro
it tells anyone who reads the program that you are comparing a pointer. It
also allows for future changes in the C standard, which could change the
definition of the NULL macro.



--- GoldED/386 2.42.G0614+
 * Origin: Everything goes the bach runter ... 
 * http://hotels-db.com
 * http://hotels-in.cc

Received on Saturday, 24 July 2004 12:24:29 UTC