pointers, tests, casts

Eric Bivona ericb at libdev
Wed Nov 23 07:44:45 AEST 1988


I have a question about tests on pointers, w.r.t. the ANSI standard
for C.

I know about casting NULL or 0 to get an appropriate nil pointer to
test against:

	char *ptr;
	void *malloc();

	ptr = (char *)malloc(256);	/* or (size_t)256?  ;-) */
	if (ptr == (char *)0) {		/* or (char *)NULL */
		perror("malloc");
		exit(1);
	}

In assignments, a 0 or NULL is cast implicitly to the correct pointer
type (I think, please correct me if I'm wrong).  What about the '=='
comparison above?  Would "(ptr == 0)" get evaluated correctly?  Or to
push it a bit further, what about the VAX-ish standard (like
*(char *)0 = 0 ;-) "(!ptr)"?  I realize that it is probably better to
explicitly cast & compare, but it did kind of make sense to say "if
(!ptr)..."

Thanks in advance,

-Eric Bivona
 DCIS Project, Dartmouth College



More information about the Comp.lang.c mailing list