I'm particularly interested in C++ if that makes any difference. I seem
to gotten by so far just fine using either. Looking back at old code, sometimes
I'm NULL Man and sometimes I'm Duff Man.. I mean Zero Man.

But I want to be consistant and would like to decide on one or the other.
Anybody have a definitive argument either way? I'm easily swayed.

WhatMeWorry Wrote:I'm particularly interested in C++ if that makes any difference. I seem
to gotten by so far just fine using either. Looking back at old code, sometimes
I'm NULL Man and sometimes I'm Duff Man.. I mean Zero Man.

In C, NULL is Zero. However, in many other languages, NULL, null, nil, or whatever is not zero, it is it's own entity. So a good habit would to always use NULL.

Not to start another language/syntax war, but if somebody is reading C source (for whatever reason) you'd expect them to be familiar with the syntax of C â€“ to code that way because it may be misunderstood by someone unfamiliar to the language seems like an unnecessary waste of time...

szymczyk Wrote:Using NULL would make your code easier to read. Setting a variable to NULL makes it obvious that the variable is a pointer. Setting a variable to 0 would not be as clear.

as if there were any difference between a pointer and an integer of some sort ,
and if something is a variable of non numeric type setting it equal to 0 should fail anyways... unless ofcourse "operator =" was overloaded for that purpose

NULL is part of the C standard but apparently (I would *never* have guessed this) --

a) You're allowed to use an integer "0" constant instead of NULL in any context (including the "if (!foo)") since NULL is defined by the standard to be "(void *)0", AND

b) The compiler will automagically convert "0" in a pointer context to the magic invalid pointer value required by the system. The only place this will fail is if you pass in something as an int and then convert it to a pointer, and there's not much the compiler can do about knowing about that (however, most systems today do use zero, so it's not a biggie).

Dan Potter Wrote:a) You're allowed to use an integer "0" constant instead of NULL in any context (including the "if (!foo)") since NULL is defined by the standard to be "(void *)0"

yeah but:

"Using NULL is a stylistic convention only; the preprocessor turns NULL back into 0 which is then recognized by the compiler, in pointer contexts, as before. In particular, a cast may still be necessary before NULL (as before 0) in a function call argument."

That is correct. However, please note that it is the *standard* stylistic convention used to denote an uninitialized address. 95% of programmers out there will mentally tag your code as being non-standard if you use zero instead of NULL. Your call dude.

My personal attitude is that I'm already going against the flow by trying to develop on a Mac. No sense in pouring any more fuel on the fire.