Quote:
|
Originally Posted by Workaphobia
That sort of makes sense. After all, jdong shows how it can be represented with operator overloads and reference returns, yet C doesn't allow reference types. Just shows how easy it is to take C++ for granted.
By the way, I remember reading somewhere that NULL (or any literal '0', since NULL is a macro) is not guaranteed to evaluate to 'false' for all implementations. This means that you may not get the result you expect when you do "if(ptr)" to test if a pointer is safe to dereference. Or maybe I have it reversed, and it's something related that isn't guaranteed... Oh well, just be careful with your if conditions.
|
In C (and C++), a conditional false is any zero value. Any non-zero value is true. NULL is defined as a constant zero (the number, not the character). This means that, assuming your pointer has a value of null, if (ptr) will always work (I should note that it's not particularly good style and you should use if ( ptr == NULL) instead).
One key thing to pointers is that they do not automatically become NULL when you free the memory allocated to them. You have to set that manually. So that may be what you're referring to.
Matt