Article View: comp.sys.atari.st
Article #39705Re: C question
From: John Stanley
Date: Mon, 18 Dec 1989 12:49
Date: Mon, 18 Dec 1989 12:49
42 lines
1662 bytes
1662 bytes
[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
Technical Details
Message-ID:
<1989Dec18.124934.1487@stag.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uwm.edu!zaphod.mps.ohio-state.edu!sunybcs!rutgers!uwvax!umn-d-ub!umn-cs!nis!pwcs!stag!daemonShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uwm.edu!zaphod.mps.ohio-state.edu!sunybcs!rutgers!uwvax!umn-d-ub!umn-cs!nis!pwcs!stag!daemon From: john@dynasoft.UUCP (John Stanley) Newsgroups: comp.sys.atari.st Subject: Re: C question Message-ID: <1989Dec18.124934.1487@stag.UUCP> Date: 18 Dec 89 12:49:34 GMT Sender: daemon@stag.UUCP (The devil himself) Organization: Mindtools ST Access Group Lines: 42