delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/29/04:41:28

Xref: news2.mv.net comp.os.msdos.djgpp:4325
From: Vincenzo Morello <morellov AT mbox DOT vol DOT it>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: printf("%s",0) - Segmentation fault
Date: Tue, 28 May 1996 22:16:06 -0700
Organization: Video On Line
Lines: 22
Message-ID: <31ABDD96.4F3D@mbox.vol.it>
References: <31A987B6 DOT F1 AT algonet DOT se>
NNTP-Posting-Host: tipa7.vol.it
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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