Mail Archives: djgpp/1996/01/10/22:15:11
>> if (!my_pointer)
>> my_pointer = (my_struct_t *)malloc( sizeof (my_struct_t) );
>>
>> Or BETTER CODE but equivalent:
>>
>> if (my_pointer == (my_struct_t *)NULL)
>> ...
>
> You shouldn't need to cast NULL, I don't think...
That's generally correct--NULL is generally #defined as something like
(void *)0, and everything can be converted to void *.
> Either way, even better than that would be:
> if (NULL == my_pointer) ...
> since this will give you a compiler error if you make a typo and
>only use one equal sign. (Otherwise, it is an assignment, which can
>cause no end of problems. It's also a PAIN to find, when you do do it.)
Really? GCC's -Wall flag enables -Wparentheses, which triggers the message
<filename>:<lineno>: warning: suggest parentheses around assignment used as
truth value
-- Aaron Ucko (ucko AT vax1 DOT rockhurst DOT edu; finger for PGP public key) | httyp!
"That's right," he said. "We're philosophers. We think, therefore we am."
-- Terry Pratchett, _Small Gods_ | Geek Code 3.1 [for explanation, finger
hayden AT mankato DOT msus DOT edu]: GCS/M/S/C d- s: a18 C++(+++)>++++ UL++>++++ P++
L++>+++++ E- W(-) N++(+) o+ K- w--- O M@ V-(--) PS++(+++) PE- Y(+) PGP(+) t(+)
!5 X-- R(-) tv-@ b++(+++) DI+ !D-- G++(+++) e->+++++(*) h!>+ r-(--)>+++ y?
- Raw text -