S61304@PRIME-A.POLY-SOUTH-WEST.AC.UK (Rat) (12/05/89)
(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! Your observations would be very much appreciated! Please reply to the net as the mailer I'm forced to use is _MEGA_ brain damaged!! :-( TMRF [A Rat with a very small brain] - Appologies to A.A. Milne :-) PS I've actually met Christopher Robin! Does that make me famous?? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % Simon Chappell (The Masked Rat Fink) % % Computing and Informatics (Final Year), Polytechnic South West (Plymouth) % % % % "Better the pride that resides, in a citizen of the world, % % than the pride that divides, when a colourful rag is unfurled." % % - Neil Peart (RUSH) % % % % JANET S61304@uk.ac.psw.pa % % BITNET S61304@pa.psw.ac.uk % % INTERNET S61304%uk.ac.psw.pa@nsfnet-relay.ac.uk % % % %Disclaimer: I have no opinion, my wife wouldn't let me! % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
engel@irisa.irisa.fr (Jean Christophe Engel) (12/05/89)
From article <8912050802.AA12717@ucbvax.Berkeley.EDU>, by S61304@PRIME-A.POLY-SOUTH-WEST.AC.UK (Rat): > Question. > > 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> > If you want to initialize an array (and a string IS an array of char) WITHIN the body of a function (whether "main" or not), you should declare it as "static" like in: static char fred[] = "Some string constant"; Thus, the array will be static, i.e. memory will be allocated once at compilation-time and initialization can be performed AT COMPILATION-time. Otherwise, i.e. if not declared static, memory will be allocated each time the function is called, and de-allocated each time the function ends, thus discarding any value the array had previously. ------------------------------------------------------------------------ | Jean-Christophe Engel (Equipe TEMIS) Phone: +33 99 36 20 00 | | IRISA Fax: 99 38 38 32 | | Campus Universitaire de Beaulieu Telex: UNIRISA 950 473F | | 35042 RENNES Cedex - FRANCE E-mail: engel@irisa.fr | ------------------------------------------------------------------------
hcj@lzaz.ATT.COM (HC Johnson) (12/06/89)
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 you can have char *fred[] = "hello"; or char fred[] = {'h','e','l','l','o','\0'}; but not char fred[] = "hello"; char *fred[] is an array of pointers, the first points to the string "hello" char fred[] is an array of chars, 6 in my example. Howard C. Johnson ATT Bell Labs att!lzaz!hcj hcj@lzaz.att.com
thamer@kayak.cis.ohio-state.edu (Mustafa Thamer) (12/06/89)
In article <875@lzaz.ATT.COM> hcj@lzaz.ATT.COM (HC Johnson) writes: >NO NO NO >you can have char *fred[] = "hello"; >or >char fred[] = {'h','e','l','l','o','\0'}; >but not >char fred[] = "hello"; Actually, K&R were not misquoted. They say on page 84 of "The C Prorogramming Language" (c)1978, "Character arrays are a special case of initialization, a string may be used instead of the braces and commas notation: char pattern[] = "the"; This is a shorthand for the longer but equivalent char pattern[] = {'t', 'h', 'e', '\0'}; ... " Evidently, the compilers are the ones who don't like it. Did you say you were from Bell Labs, where K&R came from ... -Mustafa Thamer -=- "Two days ago I saw a vehicle that'd haul that tanker. You wanna get out of here; you talk to me."
thamer@kayak.cis.ohio-state.edu (Mustafa Thamer) (12/06/89)
I just tried the following code on my laser C compiler. char pattern[] = "fred"; As a global declaration it worked fine. As a local declaration (in main) it wouldn't compile unless I put the word "static" in front of it. Then it worked fine. When declared as a static, of course it worked correctly as a global or a local. Apparently that is the way to do it as was said in a previous message. Case Closed. Mustafa Thamer -=- "Two days ago I saw a vehicle that'd haul that tanker. You wanna get out of here; you talk to me."
ron@argus.UUCP (Ron DeBlock) (12/07/89)
Someone asked why main() { char fred[] = "hello"; .... won't work, and a bunch of answers followed. Some made no sense, others were close, but I haven't seen the complete answere yet. So here it is: The error from the compiler on this line is probably something like: "No initialization of automatic agragate." This means that an aggragate structure (an arry, struct or union) that is automatically allocat cannot be pre-initialized. Automatics are any variables declared INSIDE of a function (this excludes the functions arguments). For non-aggragate variables, (int, *, cha the compiler will generate storage and code to initialize. It does not do so for aggragates. The solution is to make the variable non-automatic. Someone mentioned declaring the array as static char fred[] which will work just fine. Global variables are also not automatic, so moving the declaration outside of main() will also work. C provides the keyword "auto" to make variables automatic. I've never seen it used, since it is only valid within functi (or block) scope and it is the default. The "static" keyword has an interesting side effect: a global symbol declared as static only has file scope. This allows you to create global vaiables and functions which are visible only within a file: static int foo; int bar; static int bas() { ... } int baf() { ... } baf() and bar may be declareas extern in other files and used normally. However, foo and bas() CANNOT be referenced outside of the file! This is sometimes useful if you want to be sure that certain variables or functions cannot be accessed from other code. -- Ron DeBlock N2JSO Net: ...!rutgers!galaxy!argus US Mail: 42 Davis Street, Phillibsburg, NJ 08865 USA
pat@cscnj.csc.COM (Patrick Hester) (12/07/89)
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: + > + > 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 + + you can have char *fred[] = "hello"; + or + char fred[] = {'h','e','l','l','o','\0'}; + + but not + char fred[] = "hello"; yes you can. it sets up an array of chars including a null. fred then refers to the address of the first byte in the array and fred[n] is one char. you usually can't do this, tho, inside a function unless you declare it static. Also, it's gotta be before any program instructions on most compilers. -- "We've all been used 8=8 made loud to play loud and reused..." --<-@ "And abused." =8(/\/) rutgers!cscnj!pat "And amused!" 8=======8 (201)-562-6533
chasm@attctc.Dallas.TX.US (Charles Marslett) (12/07/89)
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
dbsuther@PacBell.COM (Daniel B. Suthers) (12/14/89)
In article <875@lzaz.ATT.COM> hcj@lzaz.ATT.COM (HC Johnson) writes: >you can have char *fred[] = "hello"; > >but not >char fred[] = "hello"; Mr. Johnson is correct; to use the second form you must use the form static char fred[] = "HELLO" or the way he has it set up as an array of pointers (sometimes). If you set it up as an array of pointers it will only work if the machine uses a linear address scheme. As this is not gauranteed in the standards, It could break if ported to another machine or different compiler. The point is to beware of utilizing side effects when writing code. The next operating system release or compiler change CAN and OFTEN DOES wreak havock with your programs. For those who have not seen it yet, the newsgroup "comp.unix.wizards" is very helpful for those just starting in C and unix. They are always eager to lend support. The newsgroup seems to be not so much for the wizards as it is for the neophytes who don't have a wizard handy to help them. ------------------------------------------------------------------------------- Dan Suthers, Analyst, Pacific Bell uucp: {backbone}!pacbell!pbeos!dbsuther ------------------------------------------------------------------------------- Youth's a difficult time, and it gets harder the longer you try to draw it out. -------------------------------------------------------------------------------
jeff@quark.WV.TEK.COM (Jeff Beadles) (12/15/89)
In article <31352@pbhya.PacBell.COM> dbsuther@PacBell.COM (Daniel B. Suthers) writes: >In article <875@lzaz.ATT.COM> > hcj@lzaz.ATT.COM (HC Johnson) writes: >>you can have char *fred[] = "hello"; >> >>but not >>char fred[] = "hello"; > >Mr. Johnson is correct; to use the second form you must use the form >static char fred[] = "HELLO" >or the way he has it set up as an array of pointers (sometimes). If >you set it up as an array of pointers it will only work if the machine >uses a linear address scheme. [in all of this, the use of "static" is optional. ] char *fred = "Hello"; is valid C practice. char *fred = "Hello"; is IDENTICAL to char fred[] = "Hello"; Either way, "fred" points to an array of char's. If your compiler does not handle this properly, then your "compiler" should be replaced with a REAL C compiler. (1/2 :-) with the declaration char *fred[], (this is the same as char **fred; ) "fred" point to an array of pointers to POINTERS to characters. This should NEVER be defined like: 'char *fred[] = "Hello"; ' >For those who have not seen it yet, the newsgroup "comp.unix.wizards" is >very helpful for those just starting in C and unix. They are always eager >to lend support. The newsgroup seems to be not so much for the wizards as >it is for the neophytes who don't have a wizard handy to help them. > Noooooooo!!!! This group is NOT for neophytes. If you have C questions, please use comp.lang.c. If you have unix questions, use comp.unix.questions. Please don't clutter comp.unix.wizards with beginners questions.
dennis@phyllis.math.binghamton.edu (dennis pixton) (12/15/89)
In article <5651@orca.wv.tek.com> jeff@quark.WV.TEK.COM (Jeff Beadles) writes: > char *fred = "Hello"; > > is IDENTICAL to > > char fred[] = "Hello"; Ummm, no. One declares and initializes a pointer, the other declares and initializes an array of characters. For a discussion see K&R, second edition, pp. 104 & 219. In particular, fred[0] = 'h' or *fred = 'h' is legal for the second declaration but not for the first (according to ANSI), while fred = "Goodbye" is legal for the first but not for the second. The original posting in this thread referred to initialization of an _automatic_ array. This was forbidden by the original K&R but is permitted by ANSI C. See K&R, second edition, p. 219. -- Dennis Pixton Department of Mathematical Sciences (607) 777-4239 SUNY-Binghamton dennis@math.binghamton.edu Binghamton, NY 13901 dpixton@bingvaxa.bitnet Dennis Pixton Department of Mathematical Sciences (607) 777-4239 SUNY-Binghamton dennis@math.binghamton.edu Binghamton, NY 13901 dpixton@bingvaxa.bitnet
unhd (Anthony Lapadula) (12/16/89)
This probably doesn't belong in this newsgroup any more than my last posting ("he," "she," and "e"), but.... In some article Jeff Beadles wrote: > > [in all of this, the use of "static" is optional. ] Not if we are talking about local variables, which we are. > char *fred = "Hello"; > > is IDENTICAL to > > char fred[] = "Hello"; Not quite. In the first case, a legal assignment would be fred = "Goodbye"; /* 'fred' now points to a different string */ This assignment fails in the second case. -- Anthony (No Sig Needed) Lapadula
john@dynasoft.UUCP (John Stanley) (12/18/89)
[S61304@PRIME-A.POLY-SOUTH-WEST.AC.UK (Rat) writes...] > 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! (Assuming that the '$' character in the line after main() is really suppost to be a '{' character on your machine and just got garbled...??) Can you please tell me where in K&R this example is used? The problem is because you can't assign a string to an unknown size array at runtime (at least not that way)... The array you give there needs to be defined with: static char fred[] = "Some string constant"; Defining the array as static tells the compiler you want the array to be created-and-initialized, as-shown, at compile-time. To assign the array at runtime, you've got a number of options, but the easiest way is: main() { char fred[22]; strcpy(fred, "Some string constant"); <rest of routine> } --- John Stanley <dynasoft!john@stag.UUCP> Software Consultant / Dynasoft Systems
2546R@FRESTP11.BITNET (12/27/89)
Date: 26 D{cembre 1989, 12:44:53 FRA
From: 2546R at FRESTP11
To: info-atari16 at wsmr-simtel20.army.mil
Lately there has been much discussion about how to have a character
string constant in a C program. Many C experts have debated on this.
Well, should I presume it is another evidence of the so-called "high
portability of the C programming language" and of its "high time saving
efficiency" ? ;-) ....
It is just another unsecurity of C.
J.M.M.
-----------------------------------------------------------------
Jean Marie de Montarby 2546R@FRESTP11 (Earn/Bitnet/Netnorth) |
|
Croire en l'avenir et aux techniques nouvelles |
Avoir le souci constant de l'utilisateur |
Pour mieux construire le lendemain des autres |