R
Ron Natalie
Peter Ammon said:The expression "someValue = 0" conveys to me that someValue is an
arithmetic type,
What does someValue++ convey to you then?
Peter Ammon said:The expression "someValue = 0" conveys to me that someValue is an
arithmetic type,
Noah Roberts said:variable = 0;
Is variable most likely a pointer or a simple variable?
variable = NULL;
Is variable most likely a pointer or a simple variable?
Now, of course you can do both with either but when you see "var = NULL"
then as a code reader you can gather much more about 'var' than if it is
"var = 0".
Peter Ammon said:The expression "someValue = 0" conveys to me that someValue is an
arithmetic type, whereas "someValue = NULL" conveys to me that someValue
is a pointer type.
I find this distinction useful for reading code, and
I'll bet a lot of other programmers do to.
Noah Roberts said:Yes, those are all very valuable distinctions yet all are representative
of the same value (though in the case of '\0' it is possible that it is
not actually 0). It is definately more readable if the programmer uses
these representiations to initialize and compare to instead of the
simple value 0.
The same goes for true and false...false is 0, so why use it, why not
just use 0 and 1?
Noah Roberts said:variable = 0;
Is variable most likely a pointer or a simple variable?
variable = NULL;
Is variable most likely a pointer or a simple variable?
Now, of course you can do both with either but when you see "var = NULL"
then as a code reader you can gather much more about 'var' than if it is
"var = 0".
NULL is not part of the language. If and when that happens, I'll
be all for it. But it wouldn't be that simple.
Jakob Bieling said:I disagree in that it is not part of the language. Though I agree that
there is room for improvement.
Actually, imo, it would suffice to make NULL
a special 'thing', just like 'true' and 'false' are.
Ivan Vecerina said:But 0. is a floating point type, and '\0' is a char type.
NULL is just 0 (or eventually (0-0), or whatever), which is misleading.
Don't you confuse that with octal zero?This said, I agree that for notational convenience, it can be nice
to distinguish a 0-pointer from a 0-integer.
To this end, I have settled with using 00 as a notation for null-pointer
values. That was a couple years ago, and I am pretty happy with it.
jeffc said:I don't understand. You are saying that NULL is part of the language?
Ron said:You can't use 0. for null, but you can use '\0'. A char literal meets the
requirements of an integer constant expression.
Peter Ammon said:The expression "someValue = 0" conveys to me that someValue is an
arithmetic type, whereas "someValue = NULL" conveys to me that someValue
is a pointer type. I find this distinction useful for reading code, and
I'll bet a lot of other programmers do to.
Similarly, I prefer "someValue = 0." if someValue is a floating point
type and "someValue = 0" if someValue is an integral type other than
char, and "someValue = '\0'" if someValue is a char.
-Peter
A reasonably decent IDE will tell you the type just by pointing at
that
variable name either by mouse or by cursor.
Jeremy said:I use Borland Delphi (IOW Pascal) quite a lot, and this defines the
reserved word "nil", which I prefer over "NULL", as it is reserved for
pointers unless you cast it, and is highlighted in the editor like
other reserved words.
Andre Kostur said:Doesn't help on a printout. I'm personally prefer using NULL to 0. I like
the extra reminder that I'm dealing with a pointer.
jeffc said:Really?
p = NULL;
There, there's a "reminder" that you're using a pointer. Now, are you
really?
Noah said:It is true that p could be anything, not necissarily a pointer. It is
also true that there are idiots out there that might use NULL to
initialize something not a pointer, or compare NULL to an integer.
However, nobody said this was foolproof; what is? What was said is that
it aids in readibility. There are plenty of practices that are normally
good that turn bad when in the wrong hands...that is just life.
NR
Noah said:Here is an interesting definition I found on the internet:
http://gcc.gnu.org/ml/gcc/1999-03/msg00155.html
/* I prefer this NIL macro to present a NIL pointer than most other
variations that I have seen (eg, NULL, 0, or (cast) 0.
Comme ci, comme ca. - larry gensch
*/
#ifndef NIL
# define NIL(type) (type *) 0
#endif
Hi group,
When filling in my Windows(tm) structs, I use
NULL for object pointers and 0 for ints, floats etc.
I found out that this compiles:
int a=NULL;
float b=NULL;
etc.
Is that valid?
Agreed. But my point was just about NULL not having a typeRon Natalie said:You can't use 0. for null, but you can use '\0'. A char literal meets the
requirements of an integer constant expression.
Don't you confuse that with octal zero?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.