On 01. 10. 2012 01:03, Philipp Klaus
On 01.10.2012 06:26, Maarten Brock wrote:
IMHO char *p = (char *)0 and char *p = NULL are not the same thing (at
least theoretically): in the first case p is a pointer to a value on
address 0 while in the second case is a pointer to an undefined address,
in sense of "nil" or "null" pointers or references in other languages.
IMO they are different too. 0 is an integer and NULL is a pointer.
Otherwise you might as well use 0.0f or 'false' to initialize a pointer.
"An integer constant expression with the value 0, or such an expression
cast to type void *, is called a null pointer constant.66) If a null
pointer constant is converted to a pointer type, the resulting pointer,
called a null pointer, is guaranteed to compare unequal to a pointer to
any object or function." (C11, 188.8.131.52)
0 is a "omnipotence", (almost) typeless constant in C: char *p = 0
is valid while char *p = 1 is not. You have to cast 1 to make a
pointer from it but the cast is not needed for 0. I don't know where
this is this coming from, probably there are historical reasons, but
I don't like it: IMHO 0 is an integer constant which should behave
as all other integer constants. I mostly try to use the flavor of 0
which best matches the context: 0 for integers, 0.0 for floats,
false (if already defined) for booleans, '\0' for characters and
NULL for pointers (puritans would even say that NULL should be
always casted to reflect the pointer type, but casts are very
dangerous so they should be avoided where ever possible).
Well, either way using 0 will yield the desired result, since the
implementation will cast it to whatever null pointer constant it prefers.
Hmmm, I can make the same statement about using NULL.
In short: IMHO it is better to use NULL instead of 0, (void *)0 or (char
*)0. (At least until somebody convinces me that I'm wrong ;-) ).
Well, 0 vs. NULL is just a stylistic matter. Personally, I prefer 0, but
the C community seems to be divided on this issue.
I prefer to use an integer value to initialize an integer variable and to
use a pointer value to initialize a pointer variable. And especially in a
structure as big as PORT you can use all the help you can get.
0 is just as much a pointer as is NULL, NULL might just be defined as 0.
The port structure is quite huge. If Visual C supported it one might wnt
to use designated initializers there to state intentions more clearly.
But since there's already a lot of NULL in the structure, I don't really
object to make the 0 I used into NULL there for now.
Yes, here we are discussing about personal styles and tastes. There
is an old Latin sentence "De gustibus non est disputandum" saying
that such a discussion makes no sense.
But there is always a BUT ;-) :
my opinion is that we should use the coding style of the existing
surrounding code. I think that sdcc mostly uses NULL for null
pointers, so IMHO this is the way to go.