NULL v. zero
Posted Jul 15, 2004 19:37 UTC (Thu) by
Ross (subscriber, #4065)
In reply to:
NULL v. zero by ikm
Parent article:
NULL v. zero
Actually, no. C++ is a little more strict. C++ doesn't have a generic
pointer type like C.
Valid C:
int *ptr=malloc(10*sizeof(int));
That's not valid C++. Instead you have to do this:
int *ptr=(int *)malloc(10*sizeof(int));
Which is really annoying because it is obvious from the context what the
type should be, and it masks the bug of not including stdlib.h before
calling malloc.
So in C++ the nil pointer is 0 (they are very adamant about this).
In C it is NULL, which can be either (void *)0 or 0. The first is
obviously better since the compiler will complain if it is used in
invalid contexts but it's a quality of implementation issue not a
conformance issue.
(
Log in to post comments)