NULL-Pointer or (T*)0-Pointers? (was Re: randomness)

Martin Weitzel martin at mwtech.UUCP
Thu Apr 12 06:40:50 AEST 1990


In article <1990Apr11.001355.25937 at athena.mit.edu> scs at adam.mit.edu (Steve Summit) writes:
>In article <4878 at helios.TAMU.EDU> john at stat.tamu.edu (John S. Price) writes:
>>srand(time(NULL));
>
>Make that
>
>	#include <sys/types.h>
>	srand((int)time((time_t)NULL));

Hmm, if I grep thru my /usr/lib/llib-lc (on a 386/ix) I find

	long	time(t) long *t; { return (0L);}

and grepping thru /usr/include/sys/types.h shows me

	typedef	long time_t; /* <time> type */

Now, could it be you really wanted to write --+
	                                      |
	                        v-------------+
	srand((int)time((time_t *)NULL));

>This is not mere pedantry; it does make a difference.  I'm
>posting this reminder only because this code matches the one time
>I ever got burned by a realio, trulio int/pointer mismatch, and
>in case there is anyone out there who still isn't convinced that
>proper pointer casts are in fact necessary in function call

True, but what you in effect did was casting the NULL to a long !?!

>arguments.  Somehow I forgot the cast one day and typed
>srand(time(NULL)) into the planet's lowest quality/popularity

Strange, normally, NULL should match any pointer type (including
long* and time_t*) but ... maybe it was implemented not quite
clean (eg `#define NULL (0)' - what is sufficient for assigning
and comparing) ... or may it be that you mixed memory modells?

BTW: In my programms I have a strong tendency *not* to use
NULL at all. The reason is that in my opinion there is not
*one* single NULL-pointer in C, but as many (T*)0-pointers, as
there are types T. In other words: I compare the result of
`fopen' to `(FILE*)0', the result `fgets' to `(char*)0' and if
I construct a binary tree with nodes of type `struct tnode'
I usually write a `#define NULLNODE ((struct tnode *)0)'.

[some lines deleted]

>Watch those NULL pointers, folks.  (Yes; prototypes might have
>helped.)

... and so should `lint'.
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list