🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.atari.st
Article #6686

Re: C question

#6686
From: Charles Marslett
Date: Thu, 07 Dec 1989 15:33
63 lines
2200 bytes
In article <875@lzaz.ATT.COM>, hcj@lzaz.ATT.COM (HC Johnson) writes:
> In article <8912050802.AA12717@ucbvax.Berkeley.EDU>, S61304@PRIME-A.POLY-SOUTH-WEST.AC.UK (Rat) writes:
> > (From "The Masked Rat Fink"   "Computing and Informatics Yr4")
> > 
> > Question.
> > 
> > (This is probably a really silly one to any C wizards, but I'm only a BASIC
> > wizard!)
> > 
> > Why wont Sozobon, Lattice or any other C compiler I've tried compile the
> > following, from K&R?
> > 
> > main()
> >      $
> >      char fred[] = "Some string constant";
> >      <rest of routine>
> >      
> > 
> > This has been confusing me for a while, as K&R (surely correct!) would
> > seem to indicate that this is indeed permissible!
> 
> NO NO NO

Wrong, YES, YES, YES (and the compilers are wrong!)

> you can have char *fred[] = "hello";

This is not legal, a correct declaration would be:

char *fred[] = {"hello", "this", "is", "Tuesday"};

an array of pointers to strings.

> or 
> char fred[] = {'h','e','l','l','o','\0'};

This is legal, and should be identical to

char fred[] = {"hello"};

or 

char fred[] = "hello";

> but not 
> char fred[] = "hello";

I have hundreds of examples of this code (properly compiling everywhere).

> char *fred[] is an array of pointers, the first points to the string "hello"

Here braces should be required, though.

> char fred[] is an array of chars, 6 in my example.
> 
> Howard C. Johnson

K&R states that the braces are not required if a string constant is used
for initialization, otherwise, they are (Don't have my copy handy, but I'll
look up page and line if anyone is really interested).

Charles Marslett
chasm@attctc.dallas.tx.us
Technical Details
Message-ID: <10516@attctc.Dallas.TX.US>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!sun-barr!newstop!texsun!pollux!attctc!chasm
References: <8912050802.AA12717@ucbvax.Berkeley.EDU> <875@lzaz.ATT.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!sun-barr!newstop!texsun!pollux!attctc!chasm
From: chasm@attctc.Dallas.TX.US (Charles Marslett)
Newsgroups: comp.sys.atari.st
Subject: Re: C question
Summary: Maybe not ANSI, but it IS K&R
Message-ID: <10516@attctc.Dallas.TX.US>
Date: 7 Dec 89 15:33:54 GMT
References: <8912050802.AA12717@ucbvax.Berkeley.EDU> <875@lzaz.ATT.COM>
Organization: The Unix(R) Connection, Dallas, Texas
Lines: 63