NO

Author Topic: I Like my NULL's ?  (Read 3286 times)

benjaminmaggi

  • Guest
I Like my NULL's ?
« on: November 21, 2005, 06:37:38 am »
some times it more *nice* to use NULL instead the 0 (zero) because you can easy remember that you dont what to pass any parameters to that particular function, but some times i get type errors and i've to do a casting from NULL to (int)NULL or else. i didn't have to do that in LCC, what can you tell me about this ?
As a new Pelles C user, i whant to say i think is a great development tool it has some verry nice features i've allways whanted in LCC it would be great if pellec decide to change to GNU, keep up the good work !
Regards from Argentina.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1917
I Like my NULL's ?
« Reply #1 on: November 21, 2005, 10:34:25 am »
The macro NULL in ISO C99 standard is defined as follows:
Quote
The macros are
NULL
which expands to an implementation-defined null pointer constant; and.....

That is:
Code: [Select]
#define NULL ((void *)0)
This means that a pointer of void could be assigned to any kind of pointer (confering to it the address 0), but not to any kind of data that is not a pointer.
Probably the compiler you are using is not fully C99 compliant, infact in old style NULL is defined simply as zero.
Anyway for easy code reading can I suggest to define your own null without type spec. I.e.:
Code: [Select]
#define NULL_VALUE 0

P.S.: Is still perfectly correct to initialize a pointer with 0 (it's compiler responsability to convert it to a null pointer of the correct type) while the type of variable (pointer) is defined in the scope. i.e.:
Code: [Select]
char *p = 0;
FILE *fp;
if ((fp=fopen("foo.foo","r")) == 0) ....

Is not valid when the compiler could not understand if the variable is a pointer (i.e. passing parameters to a function with no prototype).
See also http://www.eskimo.com/~scs/C-faq/s5.html

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2181
    • http://www.smorgasbordet.com
I Like my NULL's ?
« Reply #2 on: November 21, 2005, 08:37:21 pm »
...IIRC, C++ also defines NULL as plain 0 (zero). Which means you can easily misuse it for other things than pointers - which I have seen more times than I care to remember... Bad habit!!

Pelle
/Pelle

Greg

  • Guest
I Like my NULL's ?
« Reply #3 on: November 22, 2005, 05:19:26 am »
I agree, you should only use NULL for null pointers. Otherwise use 0 (or your own defined equivalent).