Pointer Stew

Wayne A. Throop throopw at dg-rtp.dg.com
Sat May 13 14:53:38 AEST 1989


> blm at spec0.UUCP (Bharat Lal Madhyani)

Everybody pointed out that the type (int [N][M]) is NOT implemented
as an array of pointers.  But nobody seemed to see the problem in the
first program (shown here abbreviated):

> char *path[]  = {
> 	"/usr/pgmr/junk",   	
>	[...]
>	0   };
> char **p_path = path ; /* pointer to path */
> main() { while ( **p_path ) printf("%s\n", *p_path++ ); }

Does nobody see anything wrong with the condition of that while
loop?  Anybody?  Yes, you in the back with the plastic pocket protector?
That's right, it dereferences the null pointer.  It may work on some
machines, but it is NOT portable at all.

Later examples in the same posting get this right, by replacing
the zero with an empty string.  The other way to correct it is to
make the test on (*p_path) instead of (**p_path).

--
"You'd be surprised... they're all separate little countries down there."
                                        --- Ronald Wilson Reagan
Wayne Throop      <the-known-world>!mcnc!rti!xyzzy!throopw



More information about the Comp.lang.c mailing list