🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.bugs.misc
Article #156

Re: Need help with C bug.

#156
From: Guy Harris
Date: Sat, 01 Apr 1989 23:32
48 lines
2049 bytes
>>	char	p[50];
>>	main()
>>	{
>>		printf("p = 0x%08X\n", p);
>>		if (p)
>>			printf("ok\n");
>>		else
>>			printf("bad berries\n");
>
>It seems that your interpretation of what "p" is changes from statement to 
>statement.  In the printf(), p is interpreted as a hexadecimal number,

OK, how else would you print an address on a 68K-based machine,
especially if you don't yet have "%p" in your "printf" implementation? 
(Since he said it's "Mot SVR2 based", I'm assuming it it's a 68K, and
doesn't have "%p".)

>and you are actually printing the address of p.

Yeah, I think he knows that (which is probably why he's using "%08X",
which is appropriate for a 32-bit address, rather than "%02X", which
might be more appropriate for an 8-bit "char").

>Chapter 4.3: "when an array name appears as an argument to a function, the
>location of the beginning of the array is passed")

Yup, and it happens in almost all other contexts, too - including the
expression in an "if" statement....

>At a guess:
>In the if() test you are looking at (probably) the hi order byte represented
>by the address of p[0];   this appears to be 0x0000.  

No, he's looking at the address of "p" itself; the *compiler* might be
generating code to "look at the high order byte represented by the
address of p[0]", but if it is, it's broken.

	if (p)

is, in C, equivalent to

	if (p != 0)

which, at least for "p" of pointer type, is equivalent to

	if (p != NULL)

and any compiler that *doesn't* treat them equivalently is broken.
Technical Details
Message-ID: <1362@auspex.auspex.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!auspex!guy
References: <349@iconsys.UUCP> <24971@pbhya.PacBell.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!auspex!guy
From: guy@auspex.auspex.com (Guy Harris)
Newsgroups: comp.bugs.misc
Subject: Re: Need help with C bug.
Keywords: pcc2 char array code generation
Message-ID: <1362@auspex.auspex.com>
Date: 1 Apr 89 23:32:42 GMT
References: <349@iconsys.UUCP> <24971@pbhya.PacBell.COM>
Reply-To: guy@auspex.auspex.com (Guy Harris)
Distribution: usa
Organization: Auspex Systems, Santa Clara
Lines: 48