www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/02/16:51:26

Message-Id: <31B27C89.7977@mbox.vol.it>
Date: Sun, 02 Jun 1996 22:47:53 -0700
From: Vincenzo Morello <morellov AT mbox DOT vol DOT it>
Mime-Version: 1.0
To: "Victor R. Essers" <vessers AT mpce DOT mq DOT edu DOT au>
Cc: djgpp AT delorie DOT com
Subject: Re: printf("%s",0) - Segmentation fault
References: <01BB4FB0 DOT 9776BC00 AT vessers DOT mpce DOT mq DOT edu DOT au>

Victor R. Essers wrote:
> 
> Oh, come on! Why not:
> 
> (pp ? "" : pp)
> 
> I've never seen any C implementation that treats NULL as something other than 0, so the above code is much more elegant.
> 
> - Victor R. Essers.
> 

I have a C implementation wich uses 0x1234 as the NULL value ...
No, really:
Of course, I know it works the same and it's *shorter*, but I like to
compare the pointer with the NULL value because it makes clear that pp
is a pointer and not a boolean expression.

Vincenzo Morello

> ----------
> From:   Vincenzo Morello
> Sent:   Wednesday, May 29, 1996 3:16 PM
> To:     djgpp AT delorie DOT com
> Subject:        Re: printf("%s",0) - Segmentation fault
> 
> Rikard Thunberg wrote:
> >
> > The following line will produce a "Segmentation fault" -error on some platforms/compilers:
> > printf("%s",0);
> >
> > However, some platforms/compilers will produce a string like "(Null)" or similar.
> >
> > Is it possible to tell the compiler what to do in this situation?
> >
> > /Rikard Thunberg, thunberg AT algonet DOT se
> 
> You cannot expect a compiler to act as an interpreter!
> The C compiler simply puts in the stack the pointer to
> the constant string "%s" and the integer value and then
> calls a function. (Note that 0 could be a 16 bit value
> in some environment!). The compiler knows nothing about
> the use of such parameters in the called function.
> When you get the output (null) from the printf function,
> it means your program has a BUG. If you need to treat
> a NULL string pointer as an empty string, you could
> substitute the pointer pp with the expression:
> (pp == NULL ? "" : pp)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019