Path: utzoo!attcan!uunet!uport!plocher From: plocher@uport.UUCP (John Plocher) Newsgroups: comp.sources.d Subject: Re: C Pointers and Integers Message-ID: <376@uport.UUCP> Date: 23 Jul 88 22:06:06 GMT References: <54@libove.UUCP: <701@nod2sco: <3222@bigtex.uucp: <1033@ficc.UUCP: <19789@watmath.waterloo.edu: <1305@ucsfcca.ucsf.edu: <19829@watmath.waterloo.edu: <3027@geac.UUCP> <9857@reed.UUCP> Reply-To: plocher@uport.UUCP (John Plocher) Distribution: comp Organization: Microport Systems, Scotts Valley, CA Lines: 52 >[ *lots* of stuff about C pointers and integers ] > >Yes, I know I'm stupid to get involved in this religious war. However, K&R2 >contains a detailed discussion on the subject of pointers and integers -- I shouldn't do this, but... Thanks for the overview - I think that most (if not all) the compilers on intel 286 machines (one of the more prolific 32/16 bastards out there :-) handle this without problems. One *can* do stuff like: double *dp; ... if (dp == 0) ... But because C does no checking of function arguments (K&R C, not ANSI C, for this example) the following will fail on a machine where sizeof(int) != sizeof(pointer): void foofunction( i, p, ii ) int i, ii; double *p; { if (p) printf("%d %f %d\n", i, *p, ii); else printf("%d %d\n", i, ii); } main() { foofinction( 1, 0, 2 ); } The problem (which is addressed by function prototypes in ANSI C) is that the compiler doesn't have type info for the 2nd parameter to foofunction(). i.e., what size should the 0 be converted to when it is pushed on the stack (or how many registers should it use...) It is in this case that you *MUST* cast 0 to (char *) 0 or (double *) 0. Since NULL is defined to be 0, you can also type this as (char *) NULL. Since K&R define NULL as 0, implementations which define it to anything else are wrong: #ifdef LARGE_MODEL # define NULL 0L /* or (char *)0 */ #else # define NULL 0 #endif This is the most common (bad) example I've seen in the intel CPU world. -John Plocher