On Wed, Jan 23, 2002 at 03:32:27PM -0800, Mark Vojkovich wrote:
> On Wed, 23 Jan 2002, Andy Isaacson wrote:
> > On Wed, Jan 23, 2002 at 11:24:11AM -0800, Mark Vojkovich wrote:
> > > int main()
> > > {
> > > Display *dpy;
> > > dpy = XOpenDisplay('\0'); /* $DISPLAY connection */
> >
> > That can't be right. Make it XOpenDisplay(""); instead.
> >
>
> You are mistaken. From the man page on XOpenDisplay.
>
> display_name
> Specifies the hardware display name, which
> determines the display and communications domain
> to be used. On a POSIX-conformant system, if
> the display_name is NULL, it defaults to the
> value of the DISPLAY environment variable.
>
>
> '\0' is NULL. So is 0. I'm not sure about "".
I should have read the man page before spouting off. I just looked long
enough to see the prototype at the top. You're partly right, my
"solution" was wrong, but there is something wrong with your code
fragment. I think it's misleading to conflate '\0', the ASCII NUL byte
expression, with NULL, the ANSI C null pointer constant. They're
different, and while I think the standard requires your code example to
work, it simply leads to further confusion for readers who might think
that the function is expecting a single char rather than a pointer to
char. (When I sent my first mail, I though that the standard rendered
your example nonconforming, but a little perusal revealed some
interesting facts that I had not considered.)
The rest of this mail is probably uninteresting to any normal person,
but I wrote it, and perhaps someone will benefit by reading it.
'\0' is NUL (the ASCII null byte). It sure didn't look like a valid
NULL constant to me at first. Examples of NULL include
0
(char *)0
(void *)0
(if <stddef.h> is included)
NULL
Some examples:
(1)
dpy = XOpenDisplay((char *)0);
Works on any C system (pre-ANSI, ISO89, ISO99), regardless if a
prototype for XOpenDisplay has been encountered.
(2)
dpy = XOpenDisplay(0);
Works on ANSI C systems if a prototype for XOpenDisplay has been
encountered, and probably on any UNIX system even if pre-ANSI, since
many UNIX codes assume they can do this.
(3)
dpy = XOpenDisplay(NULL);
Works on ANSI C systems if you've done "#include <stddef.h>", regardless
of the existence of a prototype for XOpenDisplay.
(4)
dpy = XOpenDisplay('\0');
is pretty questionable. I was going to say that the compiler should
warn you in this case, but apparently this gets away with being legal
due to the "sizeof('c')==sizeof(int)" clause.
It seems likely that there could be systems where this example would
fail _if sizeof('c') were 1_, but since sizeof('c')==sizeof(int), it's
unlikely that there could be a system perverse enough to make it fail.
(Sketch: A system where all arguments are passed on the stack, and are
packed with no padding space, and a prototype for XOpenDisplay has not
been encountered at the calling point, and sizeof(int) < sizeof(char *).)
-andy
_______________________________________________
Xpert mailing list
[EMAIL PROTECTED]
http://XFree86.Org/mailman/listinfo/xpert