Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!adm!dsill@NSWC-OAS.arpa From: dsill@NSWC-OAS.arpa (Dave Sill) Newsgroups: comp.lang.c Subject: Re: Why NULL is 0 Message-ID: <12436@brl-adm.ARPA> Date: 16 Mar 88 16:53:45 GMT Sender: news@brl-adm.ARPA Lines: 47 In article <800@zippy.eecs.umich.edu> Jan Wolter writes: >One other vaguely related question: Which of the following produce a null >pointer? > > int zero = 0; > char *p1 = 0; /* this is a null pointer! */ Yes. > char *p2 = zero; /* is this a null pointer? */ Maybe. K&R say assignments between pointers and ints are nonportable, as are assignments between different types of pointers. > char *p3 = (char *)zero; /* what's this? */ Exactly the same as p2. K&R define a cast as performing the conversions required to assign the operand to a variable of the type of the cast. >As I read K&R, a null pointer is only produced when a *constant* 0 is assigned >to a pointer. Almost, once generated a null pointer can be produced by assignment. I.e., char *pc1, *pc2; pc1 = 0; pc2 = pc1; >While K&R says assignment is a bitwise copy, they say explicitly typecasting >an integer to a pointer gives a machine dependent result. Thus it seems >possible that the p1, p2, and p3 could be three different pointers. No, at most two different pointers. p1 is null. p2 and p3 will be the same nonportable pointer. >(Frankly, >the more I read on this subject, the more I think K&R didn't have their minds >entirely clear on this business either.) Maybe you should read some more. ========= The opinions expressed above are mine. "Give me a pointer and I'll dereference the world." -- David Keppel